10 knowledge points a PHP beginner must master

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

Here summarizes the PHP beginner is easy to feel puzzled 10 questions, for everybody reference. 1, the page can not pass the variable get,post,session in the latest PHP version of the automatic global variable is closed, so to get from the previous page to submit the variable to use 1, the page cannot pass the variable ge

Here summarizes the PHP beginner is easy to feel puzzled 10 questions, for everybody reference. 1, the page can not pass the variable get,post,session in the latest PHP version of the automatic global variable is closed, so to get the submitted from the previous page variables to use
1. Variables cannot be passed between pages

Get,post,session Automatic global variables are turned off in the latest PHP version, so the variables to be submitted from the previous page are 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.
2, Win32 under the session 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. Display 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 called before the assignment, and the workaround is to detect or mask, such as display $foo, can be 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. No changes after changing php.ini

Restart the Web server, such as Iis,apache and so on, before the latest settings are applied.
6, sometimes the SQL statement does not work, the database operation failed. The simplest debugging method, echo the SQL, 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 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:
View Sourceprint?
function R ($file _name) {
[Email protected] ($file _name, "R");
@flock ($filenum, lock_sh);
[Email protected] ($filenum, FileSize ($file _name));
@fclose ($filenum);
return $file _data;
}
function W ($file _name, $data, $method = "W") {
[Email protected] ($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 test variables, but isset is the test of whether the variable is assigned, and empty is the test of whether a variable that has already 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.

9. MySQL Query statement contains keywords

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. How to upload multiple files at once 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:

View Sourceprint?
<form action= "" method= "POST" >
<input name= "Usefile" type= "File" >
</form>
In this way, the following tests are done on the server side

View Sourceprint?
echo "<pre>";
Print_r ($_files);
echo "</pre>";
2. Set multiple file input boxes in the form, but with different names, as follows:

View Sourceprint?
<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:

View Sourceprint?
echo "<pre>";
Print_r ($_files);
echo "</pre>";

10 knowledge points a PHP beginner must master

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.