PHP function variable scope and function return value Tutorial _php tutorial

Source: Internet
Author: User
Tags variable scope
In PHP, variables that are defined in a function, including parameters, cannot access the external variables of the function, and, by default, variables that are defined outside a function cannot be accessed by a variable.

In the PHP tutorial, variables defined in a function, including parameters, cannot access the external variables of the function, and in the default case, variables defined outside a function cannot be accessed by a variable.

See example below

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

This returns the value of $b to 3, which is a global variable in PHP, so we will now look at the PHP variable reference instance

function Str_unite (& $string)
{
$string. = ' also like blue. ';
}
$str = ' like red, ';
Str_unite ($STR);
Echo $str; Output: ' Like red, also like blue. '
?>

, which is a reference to the global variables and functions of the scope of the function, the following is a local variable of the function

$a = 1;
$b = 2;
function Sum ($a, $b)
{
$b = $a + $b;

echo $b;//3
}
Sum ();//
echo $b;//2
?>

This site original tutorial reproduced annotated source www.bkjia.com/phper/php.html


http://www.bkjia.com/PHPjc/445451.html www.bkjia.com true http://www.bkjia.com/PHPjc/445451.html techarticle In PHP, variables defined in a function, including parameters, cannot access the external variables of the function, and in the default case, variables defined outside a function cannot be accessed ...

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