The use of PHP variables: sports982002-8-13 many friends in the preparation of PHP programs sometimes on the variable always has an undetermined problem, and there are a lot of problems is caused by improper processing of variables. Let's talk about the following in the PHP variable system. I hope you can advise on the shortcomings. PHP variables include global variables and local variables. What is a global variable? Learned CJA
Use of PHP variables
Author: sports98 2002-8-13
Many of my friends sometimes have an uncertain problem with variables when writing PHP programs, and there are also many problems because
Improper processing of variables. Let's talk about the following in the PHP variable system. I hope you can advise on the shortcomings.
PHP variables include global variables and local variables.
What is a global variable?
If you have learned C/JAVA/C ++, you must understand the regions where the variables declared in main () {} are reached ~, PHP is not an interpreted statement.
Compilation language. we also know that PHP does not have the main () {} subject. Where can we declare it? In fact, the PHP page itself is a main () {} as long as it is in the page
Instead of the variables declared in the function, we call them global variables... for example:
$ Int_a = 0; // the initial global variable $ int_a assigns the value 0.
Echo "global variables:". $ int_a; // Print global variables
Function child_a () {// use of local variables
$ Int_a = 0; // This is a local variable. although it is the same as the global variable name, this variable is only visible in child_a.
Echo "From child_a:". $ int_a;
}
Function child_ B () {// call the global variable
Global $ int_a; // declare the call global variable
Echo "From child_ B:". $ int_a;
}
?>
From the above example, we can easily and clearly understand the gap between global and local variables in PHP...
We are glad that PHP provides server global variables for other CGI languages... these variables are automatically generated by the system when the page is called.
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 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 on the page) PHP4.1.0
$ _ SESSION (save the intra-site SESSION variable) PHP4.1.0
$ HTTP_POST_VARS (same as $ _ POST, according to linuxforum.net, this comment is $ _ POST more efficient than others) PHP4.1.0
$ HTTP_GET_VARS (same as $ _ GET, comment as above.) PHP4.1.0
$ _ REQUEST (including $ _ GET, $ _ POST, $ _ COOKIE, $ _ FILES) PHP4.1.0
$ _ FILES (strictly speaking, this variable is already included in $ _ POST and $ HTTP_POST_VARS, mainly to get the file variable submitted in form post mode) PHP4.1.0
$ GLOBALS (an array that saves 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 can be directly used without declaring them. of course, your PHP version must be higher than or equal to the version number marked later by them.
When using these variables, you do not need to use the global keyword to declare them...
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 declared in the same way as compiled languages. It is used directly, but if an unreasonable program variable is messy and random
I think you may not understand it yourself in a few days ~, If you only need an excessive variable, you can declare it as long as it does not conflict with other variables and complies with the naming rules.
We recommend that you use unset to delete it after use ..
PHP also provides the variable concept.
Just as someone posted a post asking this question
Below I will express this ~, In fact, this is the simplest variable method. I think it will be quick to understand the pointers and addresses in C.
In addition, you can use the name-marked value directly as a variable when you get started with PHP .), network development over time
The PHP.net development team found that many people are writing articles here. Example:
For ($ int_a; $ int_a <100; $ int_a ++ ){
Echo "I am $ int_a \ r \ n
";
}
?>
Logically speaking, the above program is like the system applying for a new variable $ int_a because the default value is not assigned, the system will assign $ int_a to 0 by default. at this time, 100 rows of I am should be printed...
But what if someone passes this variable ??? For example
Http: // url/count. php? Int_a = 99The system prints only one row. Here is just a simple introduction.
If this program operates your database or your confidential files, what you need is private variables instead of variables passed by the outside world..., I personally think the PHP Development team
The processing on this issue draws on the advantages of the compilation language ..
But don't worry, if you don't think the page is affected by external variables, and you don't need to increase the security for the moment, you can use
If ($ _ GLOBAL_ARRAY) {// an array that determines whether a global variable exists
Extract ($ _ GLOBAL_ARRAY, EXTR_PREFIX_SAME, "_ global_array"); // imports the variables in the array into the current variable table. if a conflict occurs, prefix _ global_array before the global variable.
}
Because the PHP team has improved the system security, the distributed version has set the register_global option to OFF by default since php4.2.X. if you do not need to use it, you can turn it ON
Summary:
PHP is a very good web cgi language. PHP is free and closely integrated with many databases. it can complete specific services completed by other server scripts. the middleware provided by ASP is
Can be loaded into PHP through dl () or static extension. PHP variables will become more and more perfect...
Learning is limited. please advise if any error occurs.
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.