Home >headlines >php variable scope

php variable scope

无忌哥哥
无忌哥哥Original
2018-06-28 09:18:242584browse

* There are only three scopes:

* 1. Global: Created outside the function, only used in the current script except for the function;

* 2. Local: Created inside the function, can only be used in the function, not accessible externally;

* 3. Static: Created inside the function, only used in the function, its value will not be lost after the function is executed;

$siteName = 'PHP中文网'; //全局变量

//The global variable automatically becomes a key-value pair in the global variable array, and the key corresponds to the variable

$GLOBALS['siteName']='PHP中文网'; //全局变量替代语法

* Function: It is a code segment with a specific function in the script, which can be called repeatedly

* 1. Basic syntax:

* 1.1 Function declaration: function funcnName($args){ #code...}

* 1.2 Function expression: $funcName = function ( $ages){ #code...}

* 2. Call:

* 2.1 Call by name: funcName($args) / $funcName($args)

* 2.2 Self-calling: declaration and calling are completed at the same time

* (function (args){ #code...})()

function hello()
{
  global $siteName; //引用全局变量,使用全局变量数组,不必声明引入
  $userName = 'Peter Zhu'; //局部变量
  // return '欢迎来到'.$siteName.',我是:'.$userName;
  return '欢迎来到'.$GLOBALS['siteName'].',我是:'.$userName;
}
echo hello();  //函数调用
echo &#39;<hr color="red">&#39;;

//Static variables must and can only be used in Declared and used in the function


function myStatic()
{
  static $num = 1;
  //$num++,先将$num值输出后再加1
  return &#39;第&#39;.$num.&#39;次输出&#39;.$num++.&#39;<br>&#39;;
}
echo &#39;第一次执行完成后$num值:&#39;.myStatic().&#39;<br>&#39;;

//After the first execution is completed, the value of $num is 2

echo &#39;第一次执行完成后$num值:&#39;.myStatic().&#39;<br>&#39;;

//After the second execution is completed, $ The num value is 3

echo &#39;第一次执行完成后$num值:&#39;.myStatic().&#39;<br>&#39;;

//After the third execution, the $num value is 4

echo &#39;第一次执行完成后$num值:&#39;.myStatic().&#39;<br>&#39;;

* Super global variables: $_SERVER, $_COOKIE, $_SESSION, $_GET, $_POST ,$_REQUEST

* 1. They are predefined variables, all are arrays, and can be used as they come, no declaration is required;

* 2. Cross-scope, both globally and locally (inside the function) ) can be used directly;

* 3. Cross-scope is not cross-script. The so-called super global, including global, refers to the current script file.

echo &#39;<hr color="blue">&#39;;

//Can be referenced directly globally

echo &#39;我的姓名是:&#39;.$_GET[&#39;name&#39;];

//Can also be referenced directly in the function

function sayName()
{
  //超全局变量不需要使用关键字 global 进行声明
  return &#39;我的姓名是:&#39;.$_GET[&#39;name&#39;];
}

//Call the function

echo sayName();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php judgment variableNext article:php judgment variable