Confusing 10 points of knowledge for PHP beginners

Source: Internet
Author: User
Tags flock http cookie http post mysql query

Confusing 10 points of knowledge for PHP beginners

The variable cannot be passed between "1" pages get,post,session

In the latest PHP version, the auto global variable is turned off, so the variables to be submitted from the previous page are to be obtained using $_get[' foo '],$_post[' foo '],$_session[' foo '. Of course, you can also modify the automatic global variable to ON (php.ini to register_globals = on), or it is better to force yourself to be familiar with the new wording, considering compatibility.

Note: Hyper-Global variables in PHP
Starting with PHP 4.2.0, the default value of Register_globals is off, so that many variables that can be used directly, such as $PHP _self or the session variable you set, cannot be accessed in the form of "$ variable name". This may give you a lot of the same, but it can help improve safety. To access these variables, you need to use a PHP super global variable, as follows:

$_server
Variables are set by the WEB server or directly associated with the execution environment of the current script. An array $HTTP _server_vars similar to the old array. The previous $php_self corresponds to $_server[' php_self ', and you can use Phpinfo to view your $_server variables.

$_get
A variable that is submitted to the script via the HTTP GET method. An array $HTTP _get_vars similar to the old array.

$_post
A variable that is submitted to the script via the HTTP POST method. An array $HTTP _post_vars similar to the old array.

$_cookie
A variable that is submitted to the script via the HTTP cookie method. An array $HTTP _cookie_vars similar to the old array.

$_session
The variable currently registered to the script session. An array $HTTP _session_vars similar to the old array.

$_files
A variable that is submitted to the script via an HTTP POST file upload. An array $HTTP _post_files similar to the old array.

$_env
Executes the variables that the environment submits to the script. An array $HTTP _env_vars similar to the old array.
For $_files variables: (The File Field field is "MyFile")

$_files[' myfile ' [' Name ']
The original name of the client machine file (including the path).

$_files[' myfile ' [' type ']
The MIME type of the file requires the browser to provide support for that information, such as "Image/gif".

$_files[' myfile ' [' Size ']
The size of the uploaded file, in bytes.

$_files[' myfile ' [' Tmp_name ']
Temporary file names (including paths) stored on the server after the files have been uploaded.

$_files[' myfile ' [' Error ']
The error code associated with the file upload. [' ERROR '] was added in version PHP 4.2.0.

When register_globals in PHP.ini is set to ON, the $myfile _name is equivalent to $_files[' myfile ' [' name '], $myfile _type equivalent to $_files[' myfile ' [' Type '] and so on.


The session under "2"Win32 does not work properly


php.ini default Session.save_path =/tmp
This is obviously the configuration under Linux, Win32 PHP can not read and write the session file caused by the session can not be used, change it to an absolute path, such as Session.save_path = C:\Windows\Temp.

"3" displays error message


When php.ini display_errors = on and error_reporting = E_all, all errors and hints are displayed, preferably open for error correction when debugging, if you use the previous PHP notation, most of the error information is about undefined variables. The variable is invoked before the assignment, and the workaround is to detect or mask, for example, to display the $foo, or the IF (Isset ($foo)) echo $foo or echo @ $foo

"4"header already sent


This error usually occurs when you use the header, he may be several reasons: 1, you are using the header before the Pring or Echo 2. You have a blank line 3 in front of your current file. You may include a file with a blank line or output at the end of the file.

"5" does not change after changing php.ini


Restart the Web server, such as Iis,apache and so on, before the latest settings are applied.

"6" sometimes SQL statements do not work

Failed on database operation. The simplest debugging method, echo the SQL, to see if the value of the variable can be obtained.

The difference between the "7"include and the Require


There is not much difference between the two, if the file to be included does not exist, include prompts notice, and then proceeds to execute the following statement, require prompts for a fatal error and exits. According to the test, they are all included after the Win32 platform, so the included file is best not to have include or require statements, which will cause the directory confusion. Perhaps *nux under different circumstances, not yet tested. If a file does not want to be included multiple times can be read using include_once or require_once##, 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;}

The difference between "8"isset () and Empty ()


Both are test variables, but Isset () is the test variable is assigned, and empty () is the test of whether a variable that has been assigned is empty. If a variable is not assigned the reference is allowed in PHP, but there is a notice hint. If a variable is assigned a null value, $foo = "" or $foo=0 or $foo =false, then empty ($foo) returns True, Isset ($foo) also returns true, meaning that the null value does not unregister a variable. To unregister a variable, you can use Unset ($foo) or $foo=null.

the "9"MySQL query statement contains a keyword


When PHP queries MySQL, sometimes MySQL table name or column name will have a keyword, this time the query will be error. For example, the table name is Order, the query will be error, the simple way is the SQL statement table name or column name plus ' [Tab key] to distinguish, such as SELECT * from ' order '.

"10" method of uploading multiple files one time via HTTP protocol


There are two ideas that are two implementations of the same method. Specific procedures also need to design their own
1. Set 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" >< input Name= "usefile[" type= "file" ></form >


In this way, the following tests are done on the server side

echo "< pre >";p rint_r ($_files); echo "</pre >";

2. Set 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 >";p rint_r ($_files); echo "</pre >";
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.