Variable
Use of PHP variables
Author: Sports98 2002-8-13
A lot of friends in writing PHP program sometimes there are always uncertain questions, and there are many problems because
The improper handling of variables is caused. Here on the PHP variable system said below, the deficiencies are also hope advice.
PHP variables are divided into global variables and local variables.
What is a global variable?
I've learned c/java/c++ or something. You must understand the area of the variable declared in main () {}, and PHP is not an explanatory statement
Compiled language, we also know that PHP does not exist main () {} body, where to declare? In fact, the PHP page itself is a main () {} As long as it is inside the page
Instead of the variables declared within the function, we call them global variables ... For example:
<?php
$int _a=0; Initial global variable $int_a assigns it a value of 0
echo "global variable:". $int _a; Print Global variables
function child_a () {//use of local variables
$int _a=0; Here is the local variable, although it is the same as the global variable name but this variable is only visible within child_a
echo "from Child_a:". $int _a;
}
function Child_b () {//Calling global variable
Global $int _a; Declaring calls to global variables
echo "from Child_b:". $int _a;
}
?>
From an example above, we can easily understand the difference between global and local variables in PHP ...
Another of our pleasure is that PHP provides other CGI language server global variables ... These variables are automatically generated by the system when the page is transferred
These global variables are contained in the
$_server ($HTTP _server_vars) (server-supplied related variable services) PHP4.1.0
$_env ($HTTP _env_vars) (Save related environment variables) PHP4.1.0
$_post (Saves the variable submitted using the form POST method) PHP4.1.0
$_get (Saving variables submitted using the form Get/uri method) PHP4.1.0
$_cookie (save COOKIE data obtained using the page) PHP4.1.0
$_session (Save the session variables in the site) PHP4.1.0
$HTTP _post_vars (with $_post, press linuxforum.net a comment on this is that $_post is more efficient than it is) PHP4.1.0
$HTTP _get_vars (same as $_get, comments ditto.) PHP4.1.0
$_request (including $_get,$_post,$_cookie,$_files) PHP4.1.0
$_files (strictly speaking, this variable is already contained in $_post and $http_post_vars, mainly to get the file variable submitted form POST) PHP4.1.0
$GLOBALS (save an array of all global variables) PHP3.0.0
$php _errormsg (This global variable must be opened in php.ini: Track_error=on)
The above variables are system global variables and you do not need to declare that they can be used directly, of course, your PHP version must be higher than or equal to the version number that they later marked.
When using these variables, you do not need to declare them with the Global keyword ...
<?php
function G_p () {
echo "Welcome". $_server[remote_addr]. ". Nice meet I AM ". $_server[server_addr];
Welcome 192.168.0.3. Nice meet I am 192.168.0.1
}
?>
Custom variables, PHP provides custom variables that are not the same as the compiled language, and need to be declared first. Instead of just using it directly, but if an unreasonable program variable's messy and random
I think a few days later, maybe you can't read it yourself. If you only need an excessive variable, you can freely declare that he does not conflict with other variables and that it conforms to the naming convention
It is recommended to use unset to remove it ...
PHP also provides variables for variable this concept
It's like someone posted this question before.
Here is the expression ~, in fact, this is the simplest variable variable method, I think if the pointer and address in C more familiar with the people understand this will be very fast.
<?php
$VVV = "Int_a";
$int _a= "VVV";
echo "VVV is:". $vvv; VVV is:int_a
echo "Int_a is:". $int _a; Int_a IS:VVV
Echo ' $ $vvv is: '. $ $VVV; $ $VVV IS:VVV
Echo ' $ $int _a is: '. $ $int _a; $ $int _a is:int_a
?>
Another: In the first contact with PHP when he lost can be (is the submission of variables can be directly using the value of the name tag as a variable to use.), over time, the development of the network
Php.net Development team found that there are many people in the fuss. Cases:
<?php
For ($int _a $int _a<100; $int _a++) {
echo "I am $int _a \r\n<br>";
}
?>
The above program according to logic, the procedure like system request a new variable $int_a because the default value is not assigned, the system will default to $int_a 0, should print 100 lines I am ...
But what if someone passes over the variable??? For example
http://url/count.php?int_a=99The system will print only one row at this time. I'm just saying this here.
If the program is to manipulate your database, or manipulate your confidential files, you need a private variable rather than a variable passed by the outside world ... and I personally think that the PHP development team
The process of dealing with this problem is to draw on the advantages of compiling language.
But don't worry. If you feel that the page is not affected by external variables and that you do not need to raise this level of security for the time being, you can use
if ($_global_array) {//To determine whether there is an array of global variables
Extract ($_global_array,extr_prefix_same, "_global_array"); Import a variable in an array into the current variable table and prefix the global variable with the _global_array if a conflict occurs
}
Because of the security improvements to the system by the PHP team, the distributed version has been set to off by default after php4.2.x, and you can turn it on if you don't need to use the Register_global option.
Summarize:
PHP is a very good web CGI language, PHP free and a lot of database closely combined, he can complete other server script to complete the specific services, ASP provided by the middleware in PHP
can be loaded into PHP through a DL () or by using a static extension. and PHP's variables will be more and more perfect ...
There are limits to learning, if you are not also please advise.