


The most complete and detailed PHP interview questions (with answers)
May 14, 2018 pm 02:24 PMThis 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(' ',1,'',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(“測(cè)試1″,”測(cè)試2″,”測(cè)試3″);//數(shù)組 $sarr = serialize($arr);//產(chǎn)生一個(gè)可存儲(chǔ)的值(用于存儲(chǔ))
//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);//從已存儲(chǔ)的表示中創(chuàng)建 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(“l(fā)ocalhost”,”root”,”123”) or die(“數(shù)據(jù)庫(kù)連接失敗!”); Mysql_select_db(“db_test”,$link) or die(“選擇數(shù)據(jù)庫(kù)失敗!”); $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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech
