search
HomeBackend DevelopmentPHP TutorialThe most complete and detailed PHP interview questions (with answers)

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’]

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

Header(‘location:index.php’);

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);

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

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

First method:

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

Second method:

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

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”));

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

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

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);

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);//产生一个可存储的值(用于存储)

//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 的值

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”));
}

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’],’.’));

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();

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’];
….
}

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;

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!

Statement
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
How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

How do you retrieve data from a PHP session?How do you retrieve data from a PHP session?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

How can you use sessions to implement a shopping cart?How can you use sessions to implement a shopping cart?May 01, 2025 am 12:10 AM

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function