Home > Backend Development > PHP Tutorial > The most complete and detailed PHP interview questions (with answers)

The most complete and detailed PHP interview questions (with answers)

不言
Release: 2023-03-24 15:12:01
Original
162248 people have browsed it

This article introduces the most complete and detailed PHP interview questions (with answers), which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

Related Recommended: Summary of PHP interview questions in 2019 (collection)

1. What does __FILE__ mean? (5 points)
The full path and file name of the file. If used in an include file, returns the include file name. As of PHP 4.0.2, __FILE__ always contains an absolute path, while previous versions sometimes contained a relative path.
2. How to obtain the client’s IP address? (5 points)

$_SERVER[‘REMOTE_ADDR’]
Copy after login

3. Write a statement that uses the header function to jump to the page (5 points)

Header(‘location:index.php’);
Copy after login

4. $str is a piece of html text, use Regular expression to remove all js scripts (5 points)

$pattern = ‘/<script.*>\.+<\/script>/’;
Preg_replace($pattern,’’,$str);
Copy after login

5. Write a statement to remove null values ​​​​in an array (5 points)

$arr = array(‘’,1,2,3,’’,19);
Copy after login

First method:

$array1 = array(&#39;  &#39;,1,&#39;&#39;,2,3);
print_r(array_filter($array1, "del"));
function del($var)
{
       return(trim($var)); 
}
Copy after login

Second method:

$arr=array("",1,2,3,"");
$ptn="/\S+/i";
print_r(preg_grep($ptn,$arr));
Copy after login

6. Write a function to obtain the current timestamp and a method to print the time of the previous day (Format: Year-Month-Day Hour:Minute:Second) (5 points)

Time();
Date(“Y-m-d H:i:s”,Strtotime(“-1 day”));
Copy after login

7. Write the function for encoding conversion in PHP (5 points)

Iconv(‘utf-8’,’gb2312’,$str);
Copy after login

8, $str = "1,3,5,7,9,10,20", what function can be used to convert the string str into an array containing each number? (5 points)

$arr = explode(“,”,$str);
Copy after login

9. The role of the serialize() /unserialize() function (5 points)
The explanation of serialize() and unserialize() in the PHP manual is:
serialize — Generates a representation of a storable value. The return value is a string. This string contains a byte stream representing value without losing its type and structure and can be stored anywhere.
unserialize — Create a PHP value from a stored representation
Specific usage:

$arr = array(“测试1″,”测试2″,”测试3″);//数组
$sarr = serialize($arr);//产生一个可存储的值(用于存储)
Copy after login

//Use any method (for example: if you save $sarr in a text file you You can use file_get_contents to get the stored value and save it in $newarr;

$unsarr=unserialize($newarr);//从已存储的表示中创建 PHP 的值
Copy after login

10. Write a function with the parameters as year and month, and the output result is the number of days in the specified month (5 points)

Function day_count($year,$month){
Echo date(“t”,strtotime($year.”-”.$month.”-1”));
}
Copy after login

11. The path of a file is /wwwroot/include/page.class.php. Write a method to obtain the file extension (5 points)

$arr = pathinfo(“/wwwroot/include/page.class.php”);
$str = substr($arr[‘basename’],strrpos($arr[‘basename’],’.’));
Copy after login

12. Which PHP template engine have you used? (5 points)
Smarty, the template engine that comes with thinkphp
13. Please simply write a class, instantiate this class, and write statements to call the properties and methods of the class (5 points)

Class myclass{
Public $aaa;
Public $bbb;
Public function myfun(){
Echo “this is my function”;
}
}
$myclass = new myclass();
$myclass->$aaa;
$myclass->myfun();
Copy after login

14. The table friend has been built in the local mysql database db_test. The database connection user is root and the password is 123
The friend table fields are: id, name, age, gender, phone, email
Please use php to connect to mysql, select all records with age > 20 in the friend table, print the results, and count the total number of query results. (5 points)

<?php
$link = Mysql_connect(“localhost”,”root”,”123”) or die(“数据库连接失败!”);
Mysql_select_db(“db_test”,$link) or die(“选择数据库失败!”);
$sql = “select id,name,age,gender,phone,email from friend where age>20”;
$result = mysql_query($sql);
$count = mysql_num_rows($result);
While($row = mysql_fetch_assoc($result)){
Echo $row[‘id’];
….
}
Copy after login

15. There are two tables below
user table field id (int), name (varchar)
score table field uid (int), subject (varchar) ), score (int)
The uid field of the score table is associated with the id field of the user table
Required to write the following sql statement
1) Insert a new record in the user table and insert it in the score table Two records associated with the newly added record (5 points)
2) Obtain the five records with the highest score for the user whose uid is 2 in the score table (5 points)
3) Use a joint query to obtain the name " The total score of the user named "李思" (5 points)
4) Delete the user named "李思", including the score record (5 points)
5) Clear the score table (5 points)
6 ) Delete the user table (5 points)

1). mysql_query(“insert into user(name) values(‘test’)”);
$id = mysql_insert_id();
Mysql_query(“insert into score(uid,subjext,score) values(“.$id.”,’english’,’99’)”);
2).$sql = select uid,sunjext,score from score where uid=2 order by score desc limit 0,5;
3).select s.score from score s RIGHT JOIN user u ON u.id=s.uid where u.name=’张三;
4).delete from score where uid in(select id from user where name=’李四’);
Delete from user where name=’李四’;
5).delete from score;
6).drop table user;
Copy after login

Related recommendations:

php interview question 8: The difference between innoDB and myisam

The above is the detailed content of The most complete and detailed PHP interview questions (with answers). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template