10 knowledge points that php beginners are confused about

伊谢尔伦
Release: 2023-03-02 17:40:01
Original
862 people have browsed it

【1】Variables cannot be transferred between pages. Get, post, and session automatic global variables are turned off in the latest PHP version, so to get the submitted variables from the previous page, use $_GET['foo'],$ _POST['foo'],$_SESSION['foo'] to get. Of course, you can also modify the automatic global variables to be on (php.ini is changed to register_globals = On); considering compatibility, it is better to force yourself to become familiar with the new writing method.

Note: Super global variables in PHP
Starting from PHP 4.2.0, the default value of register_globals is off. As a result, many variables that can be used directly before, such as $PHP_SELF or the SESSION variable you set cannot Accessed in the form of "$variable name", this may bring you a lot of changes, but it will help improve security. To access these variables, you need to use PHP superglobal variables, as follows:
$_SERVER
Variables are set by the web server or are directly associated with the execution environment of the current script. Similar to the old $HTTP_SERVER_VARS array. The previous $PHP_SELF corresponds to $_SERVER['PHP_SELF']. You can use phpinfo to view your $_SERVER variable.
$_GET
Variables submitted to the script via the HTTP GET method. Similar to the old $HTTP_GET_VARS array.
$_POST
Variables submitted to the script via the HTTP POST method. Similar to the old $HTTP_POST_VARS array.
$_COOKIE
Variables submitted to the script via the HTTP Cookies method. Similar to the old $HTTP_COOKIE_VARS array.
$_SESSION
The variable currently registered for the script session. Similar to the old $HTTP_SESSION_VARS array.
$_FILES
Variables submitted to the script via HTTP POST file upload. Similar to the old $HTTP_POST_FILES array.
$_ENV
Variables submitted by the execution environment to the script. Similar to the old $HTTP_ENV_VARS array.

==================================================== ===================
For the $_FILES variable: (The file domain field is "myfile")
$_FILES['myfile']['name']
Client The original name of the machine file (including path).
$_FILES['myfile']['type']
The MIME type of the file, which requires the browser to provide support for this information, such as "image/gif".
$_FILES['myfile']['size']
The size of the uploaded file, in bytes.
$_FILES['myfile']['tmp_name']
The temporary file name (including path) stored on the server after the file is uploaded.
$_FILES['myfile']['error']
The error code related to the file upload. ['error'] was added in PHP 4.2.0 version.
When register_globals in php.ini is set to on, $myfile_name is equivalent to $_FILES['myfile']['name'], $myfile_type is equivalent to $_FILES['myfile']['type'], etc. .

【2】The session under win32 does not work properly
php.ini default session.save_path = /tmp
This is obviously the configuration under linux. PHP cannot read and write the session file under win32 and the session cannot be used. Change it to An absolute path will suffice, for example session.save_path = c:windowstemp.

【3】Display error messages
When display_errors of php.ini = On and error_reporting = E_ALL, all errors and prompts will be displayed. It is best to turn it on during debugging for error correction. If you use the previous php writing method, there will be most error messages. It's about undefined variables. There will be a prompt when a variable is called before being assigned. The solution is to detect or block it. For example, if $foo is displayed, you can if(isset($foo)) echo $foo or echo @$foo

【4】header already sent
This error is usually It will appear when you use HEADER. It may be due to several reasons: 1. You PRING or ECHO before using HEADER. 2. There is a blank line in front of your current file. 3. You may have INCLUDEd a file and there is a blank line at the end of the file. This error will also occur on the line or output.

【5】No change after changing php.ini
Restart the web server, such as IIS, Apache, etc., and then the latest settings will be applied.

【6】Sometimes the sql statement does not work and the database operation fails. The easiest way to debug is to echo the SQL statement to see if the value of the variable can be obtained.

[7] The difference between include and require
There is not much difference between the two. If the file to be included does not exist, include will prompt notice, and then continue to execute the following statement, require will prompt a fatal error and exit. According to the test, under the win32 platform, they are included first and then executed, so it is best not to have include or require statements in the included files, which will cause directory confusion. Maybe the situation is different under *nux, I haven't tested it yet. If you don’t want a file to be included multiple times, you can use include_once or require_once## to read and write document data:
function r($file_name) {
$filenum=@fopen($file_name,"r");
@flock( $filenum,LOCK_SH);
$file_data=@fread($filenum,filesize($file_name));
@fclose($filenum);
return $file_data;
}
function w($file_name,$data,$method ="w"){
$filenum=@fopen($file_name,$method);
flock($filenum,LOCK_EX);
$file_data=fwrite($filenum,$data);
fclose($filenum);
return $file_data;
}

【8】The difference between isset() and empty()
Both are used to test variables, but isset() tests whether a variable has been assigned a value, while empty() tests whether a variable that has been assigned a value is empty. If a variable is referenced in PHP without being assigned a value, it is allowed, but there will be a notice. If a variable is assigned a null value, $foo="" or $foo=0 or $foo=false, then empty($foo) returns true and isset($foo) also returns true, which means assigning a null value will not log out. a variable. To unregister a variable, use unset($foo) or $foo=NULL.

【9】The mysql query statement contains keywords
When PHP queries mysql, sometimes the mysql table name or column name contains keywords, and at this time there will be errors in the query. For example, if the table name is order, an error will occur during query. The simple way is to add `[above the tab key] to the table name or column name in the SQL statement to distinguish it, such as select * from `order`.

【10】The method of uploading multiple files at one time through the HTTP protocol
has two ideas, which are two implementations of the same method. The specific procedures need to be designed by yourself
1. Set up multiple file input boxes in the form and name them with arrays, as follows:
< form action="" method="post" >
< input name= "usefile[]" type="file" >
< input name="usefile[]" type="file" >
< input name="usefile[]" type="file" >
< ; /form > Multiple file input boxes, but with different names, as follows:
< form action="" method="post" >
< input name="usefile_a" type="file" >
< input name=" usefile_b" type="file" >
< input name="usefile_c" type="file" >
< /form >
Do the same test on the server side:
echo " < pre > ";
print_r($_FILES);
echo " < /pre > ";

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!