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)

May 14, 2018 pm 02:24 PM
php interview

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!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles