PHP basic questions, php basic questions
I. Differences between several Input Functions in PHP: echo, print (), print_r (), printf (), sprintf (), and var_dump.
1. echo: the statement is not a function and there is no return value. Multiple Variable values can be output without parentheses. Arrays and objects cannot be output, and only simple types (such as int and string) can be printed );
2. print: the statement is not a function. If the return value is 1, only one variable can be output. Parentheses are not required. arrays and objects cannot be output. Only simple types (such as int and string) can be printed );
3. print_r (): a function that can print a composite type (such as string, int, float, array, and object). The output array is represented in a structure and can be expressed by print_r ($ str, true) to make print_r () return the value after print_r processing without output;
4. printf (): It is a function that formats the text and outputs it. For details, refer to the C language.
5. sprintf (): It is a function. It is similar to printf (), but does not print it. Instead, it returns formatted text (this function writes the formatted string to a variable instead of output) others are the same as printf;
6. var_dump (): It is a function that outputs the content, type, or string content, type, and length of the variable.
2. Disable the cookie session usage. Set the method and function for session expiration:
1. append the session id to the url by passing a value through the url (Disadvantage: the whole site cannot have a pure static page, because the session id of a pure Static Page cannot be passed to the next page );
2. By hiding the form, place the session id in the hidden text box of the form and submit it in the same form (Disadvantage: it is not applicable to the non-form jump directly like <a> label );
3. Directly configure the php. ini file and set session. use. trans. sid = 0 in the php. ini file to 1;
4. Save the session ID in the form of files and databases and manually call the session ID during the cross-page process;
1. setCookie (session_name (), session_id (), time () + 60 ,"/");
2. session_set_cookie_params (60); (store session in cookie );
Note: session. gc_probability/session. gc_divisor in php. ini
3. PHP methods for obtaining file content, corresponding functions
1. file_get_contents () get the content of the file (which can be obtained using the get and post methods), and read the entire file into a string;
2. Use fopen () to open the url and get the content (using the fgets () function );
3. Use the fsockopen function to open the url (which can be obtained through get and post) and get the complete data, including the header and body;
4. Use the curl library to obtain the content. before using the curl library, check php. ini to see if the curl extension has been enabled.
Iv. Differences between isset (), empty () and is_null
1. When the variable is undefined, is_null () and "parameter itself" cannot be judged as parameters, and a Notice warning error is reported;
2. empty and isset first check whether the variable exists and then check the variable value. Is_null and "parameter itself" only directly check the variable value and whether it is null. Therefore, if the variable is undefined, an error will occur!
3. isset (): returns false only if null or undefined;
4. empty (): "", 0, "0", NULL, FALSE, array (), undefined, false is returned;
5. is_null (): only determines whether it is null. If not, a warning is reported;
6. When the variable itself is used as a parameter, it is consistent with empty (), but the undefined variable is accepted, a warning is reported;
V. Roles and differences between strlen () and mb_strlen
In PHP, strlen and mb_strlen are functions used to evaluate the string length.
The built-in String Length function strlen in PHP cannot properly process Chinese strings. It only obtains the number of bytes occupied by strings. For the GB2312 Chinese encoding, strlen obtains a value of 2 times the number of Chinese characters, and for the UTF-8 encoding of Chinese, is 3 times (in the UTF-8 encoding, A Chinese Character occupies 3 bytes ). Using the mb_strlen function can better solve this problem. The usage of mb_strlen is similar to that of strlen, except that it has a second optional parameter for specifying character encoding. For example, you can use mb_strlen ($ str, 'utf-8') to get the $ str length of the UTF-8 '). If the second parameter is omitted, the internal code of PHP is used. The internal encoding can be obtained through the mb_internal_encoding () function. It should be noted that mb_strlen is not a PHP core function. before using it, make sure that. the "php_mbstring.dll" line is loaded in ini to ensure that the "extension = php_mbstring.dll" line exists and is not commented out. Otherwise, the number of undefined functions may occur. 6. How does PHP obtain the Client IP address?
$ _ SERVER ['remote _ ADDR ']; obtained through a Global Array
Getenv ('remote _ ADDR '); obtained through environment variables
The client cannot obtain the real IP address when using the proxy.