33 common php Questions and Answers _ php examples

Source: Internet
Author: User
Tags imagemagick
This article mainly introduces 33 common php interview questions and answers, which are frequently encountered during interviews. Let's take a closer look. 1. in PHP, the name of the current script (excluding the path and query string) is recorded in the predefined variable (1), and the URL linked to the current page is recorded in the predefined variable (2).

The Code is as follows:


A: echo $ _ SERVER ['php _ SELF ']; echo $ _ SERVER ["HTTP_REFERER"];

2. The execution segment will be output (3 ).

The Code is as follows:


Answer: 0

3. In HTTP 1.0, Status Code 401 indicates (4). If the "file not found" prompt is returned, the header function is available. The statement is (5 ).

The Code is as follows:


Answer: (4) Unauthorized (5) header ("HTTP/1.0 404 Not Found ");

4. The role of the Array Function arsort is (6); the role of the statement error_reporting (2047) is (7 ).

The Code is as follows:


A: (6) reverse sort the array and maintain the index relationship (7) All errors and warnings

5. Write a regular expression to ignore all JS/VBS scripts on the webpage (that is, remove the mark and its content): (9 ).

The Code is as follows:


A:/<[^>]. *?>. *? <\/>/Si

6. install PHP using the Apache module. in the http. conf file, first use the statement (10) to dynamically load the PHP module,

Then, use the statement (11) to make Apache process all files with the extension of php as PHP scripts.

The Code is as follows:


A: (10) LoadModule php5_module "D:/xampp/apache/bin/php5apache2. dll"
(11) AddType application/x-httpd-php-source. phps
AddType application/x-httpd-php. php. php5. php4. php3. phtml

7. the statement include and require can both include another file to the current file. The difference is (12). To avoid multiple inclusion of the same file, you can use the statement (13) to replace them.

The Code is as follows:


A: (12) include generates a warning when an exception occurs. require generates a fatal error (13) require_once ()/include_once ()

8. The attributes of the class can be serialized and saved to the session, so that the entire class can be restored later. The function to be used is (14 ).

The Code is as follows:


Answer: serialize ()/unserialize ()

9. A function parameter cannot be a reference to a variable unless (15) is set to on.

The Code is as follows:


A: allow_call_time_pass_reference

10. The meaning of left join in SQL is (16 ).

If tbl_user records the Student name and student ID ),

Tbl_score records the student's student ID (ID), score (score), and subject (subject ),

To print the Student name and the total score of each subject, you can use SQL statement (17 ).

The Code is as follows:


Answer: (16) Natural left Outer Join
(17) select name, count (score) as sum_score from tbl_user left join tbl_score on tbl_user.ID = tbl_score.ID group by tbl_user.ID

11 .. in PHP, heredoc is a special string and its end sign must be (18 ).

The Code is as follows:


A: The row where the end identifier is located cannot contain any other characters ";"

12. Use PHP to print the time format of the previous day in the format of 22:21:21

The Code is as follows:


A: echo date ('Y-m-d H: I: s', strtotime ('-1 Day '));

13. Differences between echo (), print (), and print_r ()

The Code is as follows:


A: echo is a language structure and has no return value. The print function is basically the same as echo. The difference is that print is a function and has a return value. print_r is a recursive print that outputs an array object.

14. How to Implement string flip?

The Code is as follows:


A: Use the strrev function. You are not allowed to write the strrev function using the built-in PHP function:
Strrev ($ str)
{
$ Len = strlen ($ str );
$ Newstr = '';
For ($ I = $ len; $ I >=0; $ I --)
{
$ Newstr. = $ str {$ I };
}
Return $ newstr;
}

15. Implement the method of intercepting Chinese strings without garbled characters.

The Code is as follows:


A: mb_substr ()

16. Use php to write a simple query, find all the content named "Zhang San" and print it out.

Table Name User

Name Tel Content Date

Zhang San 13333663366 graduated from college-10-11

Michael Zhang graduated from 13612312331 undergraduate course-10-15

Zhang Si 021-55665566 graduated from technical secondary school 2006-10-15

The Code is as follows:


A: SELECT Name, Tel, Content, Date FROM User WHERE Name = 'zhangsan'

17. How to use the following class and explain what the following means?

Class test
{
Get_test ($ num)
{
$ Num = md5 (md5 ($ num). "En ");
Return $ num;
}
}

Answer: Usage:

The Code is as follows:


$ Get_test = new test ();
$ Result = $ get_test-> Get_test (2 );

The $ num variable is returned after two md5 operations. The parameter in 2nd md5 operations adds En after the first md5 ($ num) operation.

18. Get the extension of a file in more than five ways

Requirement: dir/upload.image.jpg to find. jpg or. jpg,

The Code is as follows:


A: You can obtain the extension of a file in more than five ways.
1)
Get_ext1 ($ file_name)
{
Return strrchr ($ file_name ,'.');
}
2)
Get_ext2 ($ file_name)
{
Return substr ($ file_name, strrpos ($ file_name ,'.'));
}
3)
Get_ext3 ($ file_name)
{
Return array_pop (explode ('.', $ file_name ));
}
4)
Get_ext4 ($ file_name)
{
$ P = pathinfo ($ file_name );
Return $ p ['extension'];
}
5)
Get_ext5 ($ file_name)
{
Return strrev (substr (strrev ($ file_name), 0, strpos (strrev ($ file_name ),'.')));
}

19. How to modify the SESSION survival time

This function allows you to process and display images in various formats. Another common purpose of this function is to create images. Another option other than GD is ImageMagick, but this function library is not built in PHP and must be installed on the server by the system administrator. A: In fact, Session also provides the session_set_cookie_params () function (); to set the Session lifetime. This function must be called before the session_start () function is called:

<? Php
// Save for one day
$ LifeTime = 24*3600;
Session_set_cookie_params ($ lifeTime );
Session_start ();
$ _ SESSION ["admin"] = true;
?>

20. Write a function to convert the string "open_door" to "OpenDoor" and "make_by_id" to "MakeById ".

The Code is as follows:


A:
Function test ($ str ){
$ Arr1 = explode ('_', $ str );
// $ Arr2 = array_walk ($ arr1, ucwords ());
$ Str = implode ('', $ arr1 );
Return ucwords ($ str );
}
$ Aa = 'open _ door ';
Echo test ($ aa );
?>

21. How can I use the php environment variable to get the content of a webpage address? How can I get the IP address?

The Code is as follows:


A: $ _ SERVSR ['request _ URI ']
$ _ SERVER ['remote _ ADDR ']

22. Calculate the difference between two dates, for example, 2007-2-5 ~ Date difference of-3-6

The Code is as follows:


Answer: (strtotime ('2017-3-6 ')-strtotime ('2017-2-5')/2007*24

23. the table has three columns a B c, which are implemented using SQL statements: When Column A is greater than Column B, select column A; otherwise, select Column B, if column B is greater than column C, column B is selected; otherwise, column C is selected.

The Code is as follows:


A: select case when A> B then A else B end,
Case when B> C then B else C end
From test

24. Briefly describe how to optimize the SQL statement execution efficiency in the project. In what ways can we analyze the SQL statement performance?

The Code is as follows:


Answer: (1) select the most efficient table name order.
(2) join order in the WHERE clause
(3) Avoid using '*' in the SELECT clause '*'
(4) replace HAVING clause with Where clause
(5) Improve SQL efficiency through internal functions
(6) Avoid using computation on index columns.
(7) to improve the efficiency of group by statements, you can filter out unnecessary records before group.

25. What are the differences between mysql_fetch_row () and mysql_fetch_array?

The Code is as follows:


Mysql_fetch_row () stores a column of the database in a zero-based array. The first column is indexed 0 in the array, the second column is indexed 1, and so on. Mysql_fetch_assoc () stores a column of the database in an associated array. The index of the array is the column name, for example, if my database query returns the "first_name", "last_name", and "email" columns, the index of the array is "first_name", "last_name", and "email ". Mysql_fetch_array () can return the values of mysql_fetch_row () and mysql_fetch_assoc () at the same time.

26. What is the following code used? Please explain.

$ Date = '2014/1/123'; print ereg_replace ("([0-9] +)/([0-9] +)/([0-9] +) "," \ 2/\ 1/\ 3 ", $ date );

The Code is as follows:


This is to convert a date from MM/DD/YYYY to DD/MM/YYYY. A good friend of mine told me that this regular expression can be split into the following statements. For such a simple expression, there is no need to disassemble it. It is purely for the convenience of explanation:
// Corresponding to one or more 0-9, followed by an oblique number $ regExpression = "([0-9] + )/"; // apply one or more values to 0-9, followed by another oblique sign $ regExpression. = "([0-9] +)/"; // corresponding to one or more 0-9 $ regExpression again. = "([0-9] +)"; as for \ 2/\ 1/\ 3, it is used to correspond to parentheses. The first parenthesis is for the month,

27. What is the GD function library used?

The Code is as follows:


A: This function allows you to process and display images in various formats. Another common purpose of this function is to create images. Another option other than GD is ImageMagick, but this function library is not built in PHP and must be installed on the server by the system administrator.

28. How can I speed up page loading during your development?

The Code is as follows:


A: It is enabled only when server resources are used. Server resources are closed in time, and indexes are added to the database. static files, images, and other large files can be generated on the page on a separate server. Use the code optimization tool

29. The _ addslashes ___ function is generally used to prevent SQL injection vulnerabilities.

30. What is the difference between the transfer of value in PHP and the transfer of reference and transfer of address?

The Code is as follows:


A: If a value is assigned to a row parameter, the modification to the row parameter does not affect the value of the row parameter.
The pass-through address is a special method for passing values, but it passes an address. It is not a common case that after an int is passed, both the real parameter and the row parameter point to the same object.

31. How to use javascript to determine whether a window has been blocked

The Code is as follows:


A: Get the return value of open (). If it is null, it is blocked.

33. What kind of method do you use to solve traffic problems for high-traffic websites?

The Code is as follows:


A: First, check whether the server hardware is sufficient to support the current traffic.
Second, optimize database access.
Third, prohibit external leeching.
Fourth, control the download of large files.
Fifth, use different hosts to distribute major traffic
Sixth, use the traffic analysis and statistics software

The above is all the content of this article, hoping to help you learn php.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.