Home > Article > Backend Development > PHP common interview questions
These are interview questions I compiled from the Internet. I hope they will be helpful to you when you go out for interviews in the future.
1. Basic knowledge points
1.1 The meanings of several status codes in the HTTP protocol: 503 500 401 403 404 200 301 302. . .
200: The request is successful and the requested data is returned.
301 : Permanent redirect.
302: Temporary line redirection.
401: The current request requires user authentication.
403: The server refuses to execute the request, that is, it does not have permission.
404: The request failed, the requested data was not found on the server.
500: Server error. General server-side program execution errors.
503: The server is temporarily under maintenance or overloaded. This state is temporary.
1.2 Include require include_once The difference between require_once.
Handling failure is different:
When require fails, a fatal level error will be generated and the program will stop running. When
include fails, only a warning level error is generated and the program continues to run.
include_once/require_once handles errors in the same way as include/require.
The only difference is that when the included file code already exists, it is no longer included.
1.3 (mysql) Please write down the meaning of data type (int char varchar datetime text); What is the difference between varchar and char;
int : Numeric type
char : Fixed length string type
varchar : Variable length string type
datetime : Period time type
text : Text type
What is the difference between varchar and char:
a. The length of char is fixed, no matter how much data you store, it will always have a fixed length.
Varchar is of variable length, but it needs to add 1 character to the total length, which is used to store the position.
b. char has a fixed length, so it is much faster than varchar in terms of processing speed, but it wastes storage space.
So the storage is not large, but if you have speed requirements, you can use the char type, and vice versa. Can be instantiated with varchar type.
1.4 Debugging functions such as error_reporting use the
error_reporting() function to set the error_reporting directive in php.ini at runtime.
So you can adjust the displayed error level at any time in the program.
When using this function display_errors must be turned on.
1.5 Write code to solve the problem of multiple processes/threads reading and writing a file at the same time.
PHP does not support multi-threading. You can use PHP's flock locking function to achieve this.
$fp = fopen("/tmp/lock.txt", "w ");
if (flock($fp, LOCK_EX)) { // Perform exclusive locking
fwrite($fp , "Write something here\n");
flock($fp, LOCK_UN); // Release the lock
} else {
echo "Couldn't lock the file !";
}
fclose($fp);
1.6 Write a code to upload files.
upload.html
upload.php
$uploads_dir = '/uploads';
foreach ($_FILES["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $ _FILES["tmp_name"][$key];
$name = $_FILES["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
1.7 Mysql storage engine, the difference between myisam and innodb.
a. The MyISAM type does not support advanced processing such as transaction processing, but the InnoDB type does.
b. The MyISAM type table emphasizes performance, and its execution times are faster than the InnoDB type.
c. InnoDB FULLTEXT type indexes are not supported.
d. InnoDB does not save the specific number of rows in the table, that is to say,
When executing select count(*) from table, InnoDB has to scan the entire table to calculate how many rows there are. ,
But MyISAM only needs to simply read the number of saved rows.
e. For fields of type AUTO_INCREMENT, InnoDB must contain an index with only this field, but in the MyISAM table, it can be combined with other fields Create a joint index together.
f. When DELETE FROM table, InnoDB will not re-create the table, but will delete it row by row.
g. The LOAD TABLE FROM MASTER operation does not work for InnoDB. The solution is to first change the InnoDB table to a MyISAM table, and then change it to an InnoDB table after importing the data.
But for the additional InnoDB features used (For example, foreign key) tables are not applicable.
h. MyISAM supports table locks, and InnoDB supports row locks.
2. Web architecture, security, project experience
2.1 The MySQL database is used as the storage for the publishing system. More than 50,000 entries are added in a day. The operation and maintenance is expected to last three years. How to optimize it?
a. Design a well-designed database structure, allow partial data redundancy, and try to avoid join queries to improve efficiency.
b. Select the appropriate table field data type and storage engine, and add indexes appropriately.
c. Mysql library master-slave reading and writing separation.
d. Find regular tables to reduce the amount of data in a single table and improve query speed.
e. Add caching mechanisms, such as memcached, apc, etc.
f. For pages that do not change frequently, generate static pages.
g. Write efficient SQL. For example, SELECT * FROM TABEL is changed to SELECT field_1, field_2, field_3 FROM TABLE.
2.2 For a website with high traffic, what method do you use to solve the problem of statistics of page visits
a. Confirm whether the server can support the current traffic.
b. Optimize database access. Refer to 2.3
c. Prohibit external access to links (hotlinking), such as hotlinking of pictures.
d. Control file download.
e. Use different hosts to distribute traffic.
f. Use browsing statistics software to understand the number of visits and perform targeted optimization.
2.3 Write a regular expression to filter all JS/VBS scripts on the web page (that is, remove the tags and their contents):
Answer: /<[^>].*?>.*?<\/>/si
2.4 Use PHP to print out the time of the previous day in the format: 2006-5-10 22:21:21
Answer: echo date('Y-m-d H:i:s', strtotime('-1 day'));
2.5 The difference between echo(), print(), and print_r()
Answer: echo is a language structure with no return value; the print function is basically the same as echo The same, but the difference is that print is a function with a return value; print_r is recursive printing, used to output array objects
2.6 Method to implement Chinese string interception without garbled characters
Answer: mb_substr()
2.7 How to use PHP environment variables to get the content of a web page address? How to get the ip address?
Answer: $_SERVSR['REQUEST_URI'], $_SERVER['REMOTE_ADDR']
2.8 Find the difference between two dates, such as 2007-2-5 ~ Date difference of 2007-3-6
Answer: (strtotime('2007-3-6')-strtotime('2007-2-5'))/3600*24
2.9 How to determine whether a window has been blocked through javascript
Answer: Get the return value of open(). If it is null, it means it is blocked
The above are the PHP interview questions I compiled on the Internet. I hope it will be helpful to you when you go out to look for jobs in the future.
Related articles:
PHP interview questions (classic)
The above is the detailed content of PHP common interview questions. For more information, please follow other related articles on the PHP Chinese website!