10 Knowledge Points That Confuse PHP Beginners

[1] The variables get, post, and session cannot be passed between pages. In the latest php version, automatic global variables are closed, 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 variable to on (php.ini is changed to register_globals = On); considering the compatibility, it is better to force yourself to be familiar with the new writing method www.lampbrother.net.
Note: Super global variables in PHP Starting
from PHP 4.2.0, the default value of register_globals is off, so that many variables that can be used directly before, such as $PHP_SELF or the SESSION variable you set, cannot use "$ variable name", which may give you a lot of invariance, but helps improve security. To access these variables, you need to use PHP superglobal variables, as follows: The
$_SERVER
variable is set by the web server or directly associated with the current script's execution environment. Similar to the old array $HTTP_SERVER_VARS array. The previous $PHP_SELF corresponds to $_SERVER['PHP_SELF' ], you can use phpinfo to see your $_SERVER variable.
$_GET
variable submitted to the script via the HTTP GET method. Similar to the old array $HTTP_GET_VARS array.
$_POST
variable submitted to the script via the HTTP POST method. Similar to the old array $HTTP_POST_VARS array.
$_COOKIE
A ​​variable submitted to the script via the HTTP Cookies method. Similar to the old array $HTTP_COOKIE_VARS array.
$_SESSION
The variable currently registered with the script session. Similar to the old array $HTTP_SESSION_VARS array.
$_FILES
Variables submitted to scripts via HTTP POST file uploads. Similar to the old array $HTTP_POST_FILES array.
$_ENV
The variable that the execution environment submits to the script. Similar to the old array $HTTP_ENV_VARS array.

===================================================== ===================

For the $_FILES variable: (the file domain field is "myfile")
$_FILES['myfile']['name']
The original source of the file on the client machine name (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']
Temporary file name (including path) stored on the server after the file is uploaded.
$_FILES['myfile']['error']
and the error code associated with the file upload. ['error'] was added in PHP 4.2.0.
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. The
default session.save_path = /tmp of php.ini
is obviously the configuration under linux. Under win32, php cannot read and write session files, so the session cannot be used. Change it to an absolute path. OK, for example session.save_path = c:\windows\temp.

[3] Display error information
When php.ini's display_errors = On and error_reporting = E_ALL, all errors and prompts will be displayed. It is best to open it for error correction when debugging. If you use the previous php writing method, the error information is mostly about define variables. There will be a prompt when the variable is called before the assignment. The solution is to detect or shield, such as displaying $foo, you can if(isset($foo)) echo $foo or echo @$foo

[4] header already sent
This error usually occurs in you It appears when using HEADER, it may be for 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 INCLUDE a file with a blank line or output at the end of the file This error also occurs.

[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 to see if the value of the variable can be obtained.

[7] The difference
between . If the file to be included does not exist, include prompts a notice, and then continues to execute the following statement, require prompts a fatal error and exits. According to the test, under the win32 platform, they are all 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, it has not been tested yet. If a file does not want 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() is to test whether a variable is assigned a value, while empty() is to test whether a variable that has been assigned Is empty. If a variable is not assigned a value, it is allowed to refer to it in php, but there will be a notice prompt. 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 that assigning a null value will not log out a variable. To unregister a variable, use unset($foo) or $foo=NULL.

[9] When the mysql query statement contains the keyword
php to query mysql, sometimes the mysql table name or column name will have keywords, and the query will have errors. For example, if the table name is order, an error will occur when querying. The simple solution is to add `[tab key above] to the table name or column name in the sql statement to distinguish them, such as select * from `order`.

[10]
There are , which are two implementations of the same method. The specific program needs to be designed by yourself
1. Set up multiple file input boxes in the form, and name them with an array, as follows:
< form action="" method="post" >
< input name="usefile[]" type ="file" >
< input name="usefile[]" type="file" >
<

In this way, do the following test on the server
echo " < pre > ";
print_r($_FILES);
echo " < /pre > ";

2. Set up multiple file input boxes in the form, 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 > ";

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326816058&siteId=291194637