Global and Local scopes and Static variables in PHP, globalstatic_PHP tutorial

WBOY
Release: 2016-07-13 10:03:22
Original
880 people have browsed it

Global and Local scopes and Static variables in PHP, globalstatic

1. Local scope

function update_counter()

{

$counter++;//$counter here is a local variable, which is different from the $counter outside the function

}

$counter = 10;

update_counter();

echo $counter;

//Output: 10

2. Global scope

function update_counter()

{

global $counter;//Use the global keyword to declare within the function to obtain the $counter of the global domain

$counter++;

}

$counter = 10;

update_counter();

echo $counter;

//Output: 11

function update_counter()

{

$GLOBALS[counter]++;

}

$counter = 10;

update_counter();

echo $counter;

//Output: 11

3. Static variables

function update_counter()

{

static $counter = 0;//Use the static keyword to declare $counter, which has a local domain

$counter++;

echo "Static counter is now $countern";

}

$counter = 10;

update_counter();

update_counter();

echo "Global counter is $countern";

/*Output:

Static counter is now 1

Static counter is now 2

Global counter is 10

*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/969250.htmlTechArticleGlobal and Local scopes and Static variables in PHP, globalstatic 1. Local scope function update_counter() { $counter++;/ /Here $counter is a local variable, which is different from $counter outside the function...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template