PHP introductory training tutorial PHP variable usage
Many friends sometimes have uncertain problems with variables when writing PHP programs, and there are also many problems caused by improper handling of variables. Here is a PHP training
editor , let's talk about the PHP variable system.
PHP variables are divided into global variables and local variables.
What is a global variable?
Friends who have studied C/JAVA/C++ must know the area where the variable declared in main(){} reaches~, and PHP is an interpretive statement, not a
compiled language, and we also know that PHP does not exist main(){} body, where to declare it? In fact, the PHP page itself is a main(){} as long as the variables declared in the page
instead of the function are called global variables... For example:
<?php
$int_a=0; //Initial global variable $int_a is assigned to 0
echo "Global variable:".$int_a; //Print global variable
function child_a(){ //Use of local variable
$int_a=0; // Here is a local variable, although the name is the same as the global variable, but this variable is only visible in child_a
echo "Fromchild_a:".$int_a;
}
function child_b(){ //call global variable
global $int_a; //declare calling global variable
echo "Fromchild_b:".$int_a;
}
?>
From the above example, we can easily and clearly understand the gap between global and local variables in PHP...
Another thing we are happy about is that PHP provides other CGI language server global variables... These variables are used when the page These global variables are automatically generated by the system when they are called in.
These global variables are included in
$_SERVER($HTTP_SERVER_VARS) (related variable services provided by the server) PHP4.1.0
$_ENV($HTTP_ENV_VARS) (Save related environment variables) PHP4.1.0
$_POST ( Save the variables submitted using the Form post method) PHP4.1.0
$_GET (Save the variables submitted using the Form GET/URI method) PHP4.1.0
$_COOKIE (Save the COOKIE data obtained using the page) PHP4.1.0
$_SESSION (Save the session within the site variable) PHP4.1.0
$HTTP_POST_VARS (same as $_POST, according to a comment on linuxforum.net that $_POST is more efficient than it) PHP4.1.0
$HTTP_GET_VARS (same as $_GET, same as above.) PHP4.1.0
$ _REQUEST (including $_GET, $_POST, $_COOKIE, $_FILES) PHP4.1.0
$_FILES (strictly speaking, this variable has been included in $_POST and $HTTP_POST_VARS, mainly to obtain the file variable submitted by form post) PHP4.1.0
$GLOBALS (an array that holds all global variables) PHP3.0.0
$php_errormsg (this global variable must be turned on in php.ini: track_error=on) The
above variables are system global variables, you can use them directly without declaring them. Of course, your PHP version must be higher than or equal to The version number marked after them.
When using these variables, you don't need to declare them with the global keyword...
<?php
function g_p(){
echo "Welcome".$_SERVER[REMOTE_ADDR].". nice meet you I am".$_SERVER[ SERVER_ADDR];
// Welcome 192.168.0.3.nice meet you I am 192.168.0.1
}
?>
Custom variables, the custom variables provided by PHP are not like compiled languages, which need to be declared first. Instead, use it directly, but if an unreasonable program variable is messy,
I think after a few days, you may not be able to read it yourself~ If you only need an excessive variable, you can declare it at will. As long as it does not conflict with other variables and conforms to the naming
rules , it is recommended to use unset to delete it after use.
PHP also provides the concept
of
. The simplest variable method of variables, I think people who are familiar with pointers and addresses in C will understand this very quickly.
<?php
$vvv="int_a";
$int_a="vvv";
echo "vvvis:".$vvv; //vvv is:int_a
echo "int_ais:".$int_a; //int_a is:vvv
echo '$$vvv is:'.$$vvv; //$$vvv is:vvv
echo '$$int_a is:'.$$int_a;//$$int_a is:int_a
?>
In addition: when you first contact PHP, you can get it by what he loses (that is, The submitted variable can directly use the value of the name tag to be used as a variable.) Over time, the development of the network
PHP.net development team found that there are many people making a fuss here. Example:
<?php
for($int_a;$int_a<100;$int_a++){
echo "I am $int_a\r\n<br>";
}
?>
Logically speaking, the above program is like applying a new one to the system Since the variable $int_a is not assigned a default value, the system will assign 0 to $int_a by default. At this time, 100 lines of i am should be printed...
But what if someone passes this variable? For example, http://url/count .php?int_a=99 At this time, the system will only print one line. I'm just here to briefly say that
if this program is to operate your database, or operate your confidential files, what you need are private variables instead of variables passed from the outside world...
But don't worry, if you think the page is not affected by external variables, and you don't need to improve this level of security temporarily, 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 the variables in the array into the current variable table, if there is a conflict, add the _global_array prefix to the global variables
}
Because the PHP team has improved the security of the system, the distributed version is from php4.2 After .X, the register_global option has been set to OFF by default. If you don't need to use it, you can turn it ON
. Summary:
PHP is an excellent WEB CGI language. PHP is free and closely integrated with many databases. It can complete other server scripts. The completed specific service, the middleware provided by ASP
, can be loaded into PHP through dl() in PHP or by using static extension. And PHP's variables will become more and more perfect...
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326536754&siteId=291194637
Recommended
Ranking