Variable scopes in PHP and JS

Source: Internet
Author: User

One, the variable scope in PHP

For most PHP variables there is only one scope. Local variable scopes are used in user-defined functions. All variables used within the function are set to local variables. For example:

<?php$a=1;function Test () {   echo $a;} Test ();? >

This program does not output anything because the Echo statement outputs a local variable $a, and the $a within the function has never been assigned a value. You may notice a slight difference from C, where a global variable can be referenced directly within a function, unless it is overridden by a local variable. Because this makes it possible for people to pay no attention to modifying the values of global variables. In PHP, the use of global variables within a function must be explicitly described. For example:

<?php$a=1; $b =2;function Sum () {    global $a, $b;    $b = $a + $b;} Sum (); Echo $b;? >

The above program will output "3". By declaring $ A and $b inside the function as global variables , all variables refer to the global. There is no limit to the number of global variables that a function can manipulate. The second way to access global variables is to use PHP-defined $globals arrays. The above example can be written as:

<?php$a=1; $b = 2; function Sum () {     $GLOBALS ["b"] = $GLOBALS ["a"] + $GLOBALS ["B"];} Sum (); echo $b;? >
$GLOBALS array is a key value that is used as the name of the global variable and the value of the variable is the associative array that corresponds to the value of the array element. Another important notable area of scope is the static variable.
A static variable exists in the local function, but its value is not lost when the program leaves the function.

The self-addition of $a++ has no effect because the variable $ A is freed after the function call ends. To make the Count program count effectively without losing the current count result,
$a to be known as a static variable:
<?phpfunction Test () {     $a =0;     echo $a;     $a + +; }?>

Now, every time you call the test () function, it prints a value of $ A and increments its value. Static variables are essential when using recursive functions. A recursive function is a function that calls itself. Be very careful when writing recursive functions, because the number of cycles is uncertain.
You must ensure that there are sufficient conditions to end the recursion process. The following is a simple recursive function that counts to 10:
<?phpfunction Test () {     static $count =0;     $count + +;     echo $count;     if ($count <) {        test ();     }}? >

  

Second, the variable scope of JS

Reference

JS Scope

Variable scopes in PHP and JS

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.