Php defines global variables, static variables, and local variables. Example-PHP source code

Source: Internet
Author: User
Ec (2); & nbsp; $ globals array & nbsp; functiontestfunction () {& nbsp; echo $ globals [& quot; php_self & quot;]; & nbsp ;}& nbsp; testfunction (); & nbsp; this program uses global definition & nbsp; functiontes script ec (2); script

// $ Globals Array

Function testfunction (){
Echo $ globals ["php_self"];
}
Testfunction ();

// Global definition is used in this program
Function testfunction (){
Global $ s;
Echo $ s;
}
$ S = 'this is www.111cn.net ';
Testfunction ();

// Example of static variables
Function testfunction (){
Static $ mystr;
$ Mystr. = "111cn.net ";
Echo $ mystr ."
";
}
Testfunction (); // 111cn.net
Testfunction (); // 111cn.net111cn.net
Testfunction (); // 111cn.net111cn.net111cn.net

// Common variable
Function testfunction (){
$ Mystr. = "www.111cn.net ";
Echo $ mystr ."
";
}
Testfunction (); // www.111cn.net

// Let's briefly introduce the super global variables in php.

// $ GLOBALS contains a reference to a variable that is valid globally for each current script.

$ GLOBALS ['SITE'];

// $ _ SERVER variables are set by the web SERVER or directly associated with the execution environment of the current script

Echo $ _ SERVER ['document _ root'];

// $ _ Get url request the variable submitted to the script

Echo $ _ GET ['ac']

// $ _ POST the variables submitted to the script by the http post method

Echo $ _ POST ['AB']

// $ _ COOKIE the variables submitted to the script by the HTTP Cookies Method

Setcookie ('load', 'www .111cn.net ', time () + 3600*24,'/', '192. 168.0.110 ');
$ Cookis = $ _ COOKIE ['loaddomain '];

// $ _ FILES variables submitted to the script after the http post file is uploaded



Output value:
Print_r ($ _ FILES ['file']);

Array
(
[Name] => 45457.jpg
[Type] => image/pjpeg
[Tmp_name] => C: WINDOWSTempphpD7.tmp
[Error] => 0
[Size] = & gt; 10974
)

// $ _ ENV is not recommended
// $ _ REQUEST is submitted to the script variables by GET, POST, and COOKIE mechanisms. Therefore, this array is not safe and inefficient.

$ _ REQUEST ['bb '] // the data that will be automatically submitted is post, in the get form

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.