Summary of PHP classic interview questions (continued)

Source: Internet
Author: User

1. How can I intercept Chinese strings without garbled characters?

A: function GBsubstr ($ string, $ start, $ length ){
If (strlen ($ string)> $ length ){
$ Str = null;
$ Len = $ start + $ length;
For ($ I = $ start; $ I <$ len; $ I ++ ){
If (ord (substr ($ string, $ I, 1)> 0xa0 ){
$ Str. = substr ($ string, $ I, 2 );
$ I ++;
} Else {
$ Str. = substr ($ string, $ I, 1 );
}
}
Return $ str .'...';
} Else {
Return $ string;
}
}

2. Use PHP to write the code that shows the Client IP address and Server IP address?
A: print the Client IP Address: echo $ _ SERVER ['remote _ ADDR ']; or: getenv ('remote _ ADDR ');
Print Server IP: echo gethostbyname ("www.bolaiwu.com ");

3. How to modify the session survival time?
Answer: Method 1: Set session. gc_maxlifetime in php. ini to 9999 to restart apache.
Method 2: $ savePath = "./session_save_dir /";
$ LifeTime = hour * second;
Session_save_path ($ savePath );
Session_set_cookie_params ($ lifeTime );
Session_start ();
Method 3: setcookie () and session_set_cookie_params ($ lifeTime );

4. There is a webpage address, such as the php development resources home page:Http://www.phpres.com/index.html, How to get its content?
Answer: method 1 (for PHP5 and later versions ):
$ Readcontents = fopen ("http://www.phpres.com/index.html", "rb ");
$ Contents = stream_get_contents ($ readcontents );
Fclose ($ readcontents );
Echo $ contents;
Method 2:
Echo file_get_contents ("http://www.phpres.com/index.html ");

5. In HTTP 1.0, Status Code 401 indicates (?); If the message "file not found" is returned, the header function is available. The statement is (?);?
A: Status 401 indicates that it is not authorized. header ("Location: www. XXX. php ");

6. What about MVC?
A: applications completed by models, views, and controllers
The model sends the functions to be implemented to the Controller, and the Controller receives the organizational functions and passes them to the view;

7. The difference between passing a value in PHP and transferring a reference. When will the value be passed for reference?
A: pass by value: any changes to the value within the function range will be ignored outside the function.
Pass by reference: any changes to values within the function range can also reflect these changes outside the function.
Advantages and disadvantages: When passing by value, PHP must copy the value. This is especially costly for large strings and objects.
Transferring by reference does not require copying values, which is good for performance improvement.

8. What is the role of error_reporting in PHP?
A: Set the error level and error message return.

9. Please write a function to verify that the email format is correct?
A: function checkemail ($ email)
{
$ Pregemail = "/[a-z0-9] * [-_ \.]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [\.] [A-Z] {2, 3} ([\.] [A-Z] {2 })? /I ";
Return preg_match ($ pregemail, $ email );
}

10. What is the function in the JS form pop-up dialog box? What is the input focus function?
A: The displayed dialog box is alert (), Prompt (), and confirm ()
Obtain the input focus ();

11. What is the JS steering function? How to introduce an external JS file?
A: window. location. href, <script type = "text/javascript" src = "js/js_function.js"> </script>

12. What is the difference between mysql_fetch_row () and mysql_fetch_array?
A: mysql_fetch_row is a one-row array from the result set, which is used as an enumeration.
Mysql_fetch_array is used to retrieve an array from the result set as an associated array or a number array. Both of them are required;

13. What is the difference between mysql_fetch_row () and mysql_fetch_array?
A: mysql_fetch_row is a one-row array from the result set, which is used as an enumeration.
Mysql_fetch_array is used to retrieve an array from the result set as an associated array or a number array. Both of them are required;

14. write out the meaning of the Data Type (INT char varchar datetime text). What is the difference between varchar and Char?
A: int Is a numeric string with a fixed char length. It is a string of the actual length of varchar, datetime, and text.
The location of char is fixed to the length set for table creation, and varchar is a variable-length character.

15. What is the function used to obtain the total number of query result sets?
A: mysql_num_rows ($ result );

17. Write the PhP5 constructor and destructor?
A: __construct, _ destruct

18. Write a function and retrieve the file extension from a standard URL as efficiently as possible?
Example: http://www.sina.com.cn/abc/de/fg.php? Id = 1 need to retrieve php or. php
Answer 1:
Function getExt ($ url ){
$ Arr = parse_url ($ url );

$ File = basename ($ arr ['path']);
$ Ext = explode (".", $ file );
Return $ ext [1];
}
Answer 2:
Function getExt ($ url ){
$ Url = basename ($ url );
$ Pos1 = strpos ($ url ,".");
$ Pos2 = strpos ($ url ,"? ");
If (strstr ($ url ,"? ")){
Return substr ($ url, $ pos1 + 1, $ pos2-$ pos1-1 );
} Else {
Return substr ($ url, $ pos1 );
}
}

19. Write a function to calculate the relative paths of the two files?
For example, $ a = '/a/B/c/d/e. php ';
$ B = '/a/B/12/34/c. php ';
The relative path of $ B relative to $ a should be http://www.cnblogs.com/c/dadd ()
A: function getRelativePath ($ a, $ B ){
$ ReturnPath = array (dirname ($ B ));
$ ArrA = explode ('/', $ );
$ ArrB = explode ('/', $ returnPath [0]);
For ($ n = 1, $ len = count ($ arrB); $ n <$ len; $ n ++ ){
If ($ arrA [$ n]! = $ ArrB [$ n]) {
Break;
}
}
If ($ len-$ n> 0 ){
$ ReturnPath = array_merge ($ returnPath, array_fill (1, $ len-$ n ,'..'));
}

$ Returnpath = array_merge ($ returnpath, array_slice ($ ARRA, $ n ));
Return implode ('/', $ returnpath );
}
Echo getrelativepath ($ A, $ B );

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.