Home > php教程 > php手册 > PHP basic interview questions

PHP basic interview questions

WBOY
Release: 2016-07-21 14:52:59
Original
1007 people have browsed it

1. The differences between several input functions echo, print(), print_r(), printf(), sprintf(), var_dump() in PHP.

 1.echo: It is a statement, not a function. It has no return value and can output multiple variable values ​​without parentheses. Arrays and objects cannot be output, only simple types (such as int, string) can be printed;

 2.print: It is a statement, not a function. It has a return value of 1. It can only output one variable. It does not necessarily require parentheses. It cannot output arrays and objects. It can only print simple types (such as int, string);

 3.print_r(): It is a function that can print composite types (such as string, int, float, array, object, etc.) when outputting the array, it will be represented by a structure, and print_r can be used through print_r($str,true) ()Does not output but returns the value processed by print_r;

 4.printf(): It is a function that formats text and outputs it. Please refer to C language

 5.sprintf(): It is a function, similar to printf(), but does not print, but returns formatted text (this function writes the formatted string into a variable instead of outputting it) Others Same as printf;

 6.var_dump(): It is a function that outputs the content and type of a variable or the content, type, and length of a string.

2. How to use a session that disables cookies, and set the session expiration method and corresponding function:

1. Pass the value through the url and append the session id to the url (disadvantage: there cannot be purely static pages in the entire site, because the purely static page session id will not be passed to the next page);

2. By hiding the form, put the session id in the hidden text box of the form and submit it together with the form (disadvantage: it is not applicable to non-form situations where the tag directly jumps);

3. Configure the php.ini file directly, and set session.use.trans.sid = 0 in the php.ini file to 1;

4. Save the session ID in a file, database, etc., and call it manually during the cross-page process;

 1.setCookie(session_name(),session_id(),time()+60,"/");

 2.session_set_cookie_params(60);(Save session in cookie);

Note: session.gc_probability / session.gc_divisor in php.ini

3. PHP method of obtaining file content, corresponding function

 1.file_get_contents() Get the contents of the file (can be obtained with the get and post methods), and read the entire file into a string;

 2. Use fopen() to open the url and obtain the content by get method (with the help of fgets() function);

3. Use the fsockopen function to open the url (can be obtained by get and post methods) and obtain the complete data by get method, including header and body;

4. Use the curl library to obtain content. Before using the curl library, you need to check php.ini to see if the curl extension has been turned on

4. The difference between isset(), empty() and is_null

1. When the variable is undefined, is_null() and "the parameter itself" are not allowed to be judged as parameters, and a Notice warning error will be reported;

 2. Empty and isset will first check whether the variable exists, and then detect the variable value. And is_null and "the parameter itself" just check the variable value directly to see if it is null, so if the variable is not defined, an error will occur!

 3. isset(): Only when null and undefined, returns false;

 4. empty(): "", 0, "0", NULL, FALSE, array(), undefined, all return false;

5. is_null(): only determines whether it is null, and will report a warning if it is not defined;

6. The variable itself is used as a parameter, which is consistent with empty(), but when accepting undefined variables, a warning will be reported;

5. The functions and differences between strlen() and mb_strlen

 In PHP, strlen and mb_strlen are functions to find the length of a string

  PHP’s built-in string length function strlen cannot correctly handle Chinese strings. It only gets the number of bytes occupied by the string. For the Chinese encoding of GB2312, the value obtained by strlen is 2 times the number of Chinese characters, while for UTF-8 encoded Chinese, it is 3 times (under UTF-8 encoding, one Chinese character occupies 3 bytes).
Using the mb_strlen function can better solve this problem. The usage of mb_strlen is similar to strlen, except that it has a second optional parameter to specify the character encoding. For example, to get the length of the UTF-8 string $str, you can use mb_strlen($str,'UTF-8'). If the second parameter is omitted, PHP's internal encoding will be used. The internal encoding can be obtained through the mb_internal_encoding() function.  
It should be noted that mb_strlen is not a core function of PHP. Before using it, you need to make sure that php_mbstring.dll is loaded in php.ini, that is, make sure that the line "extension=php_mbstring.dll" exists and has not been commented out, otherwise it will An undefined function problem occurred.
6. How to obtain the client’s IP address in PHP

  $_SERVER['REMOTE_ADDR'] ; Obtained through global array

 getenv('REMOTE_ADDR') ; Get

through environment variables

When the client uses a proxy, it cannot obtain the real IP address

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template