Fourteen problems that most headache PHP beginners

web front end five thousand seven hundred and fourteen 13 years ago (2011-04-14)

【1】 The variables get, post, and session cannot be transferred between faces. In the latest PHP version, the automatic global variables are closed, so to obtain the submitted variables from the previous side, you need to use $_GET ['foo '], $_POST ['foo'], $_SESSION ['foo ']. Of course, you can also change the automatic global variable to On (php. ini to register_globals=On); Considering compatibility, it's better to force yourself to be familiar with the new writing method.   

【2】 An error occurs when Apache uses the get method to pass Chinese parameters in Win:

  test.php ? A=Hello&b=You too

Passing parameters will result in an internal error
   
Solution: "test. php? A=". urlencode (Hello) "&b=". urlencode (you too)

【3】 The session under Win does not work properly

Php. ini default session.save_path=/tmp

This is obviously a configuration under Linux. PHP cannot read or write the session file under Windows, so the session cannot be used. Just change it to an absolute path, for example, session.save_path=c: windows emp

【4】 Display error message

When php. ini display_errors=On and error_reporting=E_ALL, all errors and prompts will be displayed. You'd better open it for error correction when debugging. If you use the previous php writing method, the error information is mostly about undefined variables. There will be a prompt when the variable is called before it is assigned. The solution is to detect or mask it.   

For example, if $foo is displayed, you can if (isset ($foo)) echo $foo or echo @ $foo

【5】 Email cannot be sent by mail() under Win

Sendmail configured in Linux can be sent, and smtp needs to be called in Win The server To send e-mail, modify the SMTP=ip//ip of php. ini is a smtp server without authentication function (hard to find on the Internet)

The best way to send an email in PHP is to use a socket to send it directly to the email server of the other party instead of a forwarding server.   

【6】 If no password is set for the initial installation of MySQL, update mysql.user set password="yourpassword" where user="root" should be used to modify the password

【7】header already sent  

This error usually occurs when you use HEADER. It may be caused by several reasons: you are PRING or ECHO before using HEADER. There is a blank line in front of your current file. You may INCLUDE a file, and there is a blank line at the end of the file or the output may also cause this error.!   

【8】 No change after changing php. ini

Restart the web server, such as IIS and Apache, before applying the latest settings.   

【9】 PHP is installed on 00 (ISAPI installation method is recommended)

PHP isapi.dll of PHP seems to conflict with 00. It can only be installed in CGI mode

Step 1, first www.php . net in an installation program, I installed: php -- Installer.exe, you can also find the latest version and install php -- Before installer.exe, make sure that your IIS 6.0 is started and accessible. After installation, click the default website>Application Configuration.   

Step 2: click Web Service Extension ->New Web Service Extension.   

Step 3: Extension -->php, then add

Step 4: Find the path of php. exe and add it.   

Step 5: OK!   
   
Step 6: Select the php service extension, and then click Allow.   

【10】 Sometimes SQL statements do not work data base The operation failed. The simplest debugging method is to echo the SQL statement and check whether the variable value can be obtained.   

【11】 Difference between include and require

There is not much difference between the two. If the file to be included does not exist, include prompts notice, and then continue to execute the following statement, require prompts fatal errors and exits.   

According to my test, they are all included before execution on the win platform, so it is better not to have include or require statements in the included files, which will cause directory confusion. Maybe the situation is different under * nux. It hasn't 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 the 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;   
}  
【12】 The difference between isset() and empty()

Both are used to test variables, but isset() tests whether a variable is assigned, and empty() tests whether a variable that has been assigned is empty.   

If a variable is referenced without being assigned a value, it is allowed in PHP, but there will be a notice prompt. If a variable is assigned a null value, $foo="" or $foo=0 or $foo=false, empty ($foo) returns true, and isset ($foo) returns true, that is, the null value will not be used to write off a variable.   
   
To unregister a variable, you can use unset ($foo) or $foo=NULL

【13】 MySQL query statements contain keywords

When PHP queries MySQL, sometimes MySQL table names or column names will have keywords, and then there will be errors in the query. For example, if the table name is order, errors will occur when querying. The simple way is to add the '[tab key] to the table or column names in the SQL statement to distinguish them, for example, select * from ` order`

【14】 Method of uploading multiple files at one time through HTTP protocol

There are two ideas, two implementations of the same method. The specific procedures need to be designed by ourselves.   

Set multiple file input boxes in the form and name them with arrays, as follows:

<form action="" method=post>  
<input type=file name=usefile[]>  
<input type=file name=usefile[]>  
<input type=file name=usefile[]>  
</form>  
In this way, do the following tests on the server side:

echo "<pre>";   
print_r($_FILES);   
echo "</pre>";   
. Set multiple file input boxes in the form with different names, as follows:

<form action="" method=post>  
<input type=file name=usefile_a>  
<input type=file name=usefile_b>  
<input type=file name=usefile_c>  
</form>  
Do the same test on the server side:

echo "<pre>";   
print_r($_FILES);   
echo "</pre>";