Home headlines php variable scope

php variable scope

Jun 28, 2018 am 09:18 AM

* 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;

1

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

Copy after login

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

1

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

Copy after login

* 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...})()

1

2

3

4

5

6

7

8

9

function hello()

{

  global $siteName; //引用全局变量,使用全局变量数组,不必声明引入

  $userName = 'Peter Zhu'; //局部变量

  // return '欢迎来到'.$siteName.',我是:'.$userName;

  return '欢迎来到'.$GLOBALS['siteName'].',我是:'.$userName;

}

echo hello();  //函数调用

echo '<hr color="red">';

Copy after login

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


1

2

3

4

5

6

7

function myStatic()

{

  static $num = 1;

  //$num++,先将$num值输出后再加1

  return '第'.$num.'次输出'.$num++.'<br>';

}

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

Copy after login

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

1

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

Copy after login
Copy after login
Copy after login

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

1

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

Copy after login
Copy after login
Copy after login

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

1

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

Copy after login
Copy after login
Copy after login

* 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.

1

echo '<hr color="blue">';

Copy after login

//Can be referenced directly globally

1

echo '我的姓名是:'.$_GET['name'];

Copy after login

//Can also be referenced directly in the function

1

2

3

4

5

function sayName()

{

  //超全局变量不需要使用关键字 global 进行声明

  return '我的姓名是:'.$_GET['name'];

}

Copy after login

//Call the function

1

echo sayName();

Copy after login
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP Notice: Undefined variable:Solution PHP Notice: Undefined variable:Solution Jun 25, 2023 pm 04:18 PM

PHP Notice: Undefined variable:Solution

Solution to PHP Notice: Undefined variable: result Solution to PHP Notice: Undefined variable: result Jun 22, 2023 pm 01:32 PM

Solution to PHP Notice: Undefined variable: result

PHP Notice: Undefined variable: arr in solution PHP Notice: Undefined variable: arr in solution Jun 22, 2023 am 10:21 AM

PHP Notice: Undefined variable: arr in solution

How to pass PHP variables by reference How to pass PHP variables by reference Aug 26, 2023 am 09:01 AM

How to pass PHP variables by reference

PHP Notice: Undefined variable: sql solution PHP Notice: Undefined variable: sql solution Jun 23, 2023 am 08:51 AM

PHP Notice: Undefined variable: sql solution

How to use numeric variables in PHP How to use numeric variables in PHP Sep 13, 2023 pm 12:46 PM

How to use numeric variables in PHP

Variables and data types in PHP Variables and data types in PHP May 26, 2023 am 08:40 AM

Variables and data types in PHP

What are the names of php variables? What are the names of php variables? Jul 24, 2023 pm 02:08 PM

What are the names of php variables?