达内php面试题,2015经典php笔试题与答案
时间: 2023-07-31 09:09:21 浏览: 231
php经典面试题及答案
3星 · 编辑精心推荐
以下是一些经典的PHP笔试题及其答案:
1. PHP中如何打印输出变量的值?
答案:可以使用echo或print语句来打印输出变量的值,例如:
```
$var = "Hello, world!";
echo $var;
print $var;
```
2. PHP中如何定义一个常量?
答案:可以使用define()函数来定义一个常量,例如:
```
define("PI", 3.14159);
echo PI;
```
3. PHP中如何连接MySQL数据库?
答案:可以使用mysqli_connect()函数来连接MySQL数据库,例如:
```
$host = "localhost";
$user = "username";
$password = "password";
$database = "my_db";
$conn = mysqli_connect($host, $user, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
```
4. PHP中如何执行SQL查询语句?
答案:可以使用mysqli_query()函数来执行SQL查询语句,例如:
```
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}
} else {
echo "0 results";
}
```
5. PHP中如何上传文件?
答案:可以使用$_FILES数组来上传文件,例如:
```
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
```
这些是一些经典的PHP笔试题及其答案,希望对你有所帮助。
阅读全文