PHP Local variables and global variables

Source: Internet
Author: User

First, local variables

Definition: A variable that is declared inside a function and can only be called inside a function.



Note: Parameters are also one of the local variables.



Demo1:

1 function demo1 () {
2 $age = 10;
5;
4
5 echo $age; Undefined Local variables can no longer be called outside the function



Second, global variables

Definition: Declared outside the function, you can use the Global keyword call inside the function. (Note order)



Demo2:

$name = "Linwu";

function Demo2 () {
Global $name;
Echo $name;
}

Demo2 ();



It is important to note, however, that the declaration of a global variable must precede a function call.



DEMO3:

function Demo2 () {
Global $name;
Echo $name;
}
$name = "Linwu";//Output Linwu
Demo2 ();



Demo4:

function Demo2 () {
Global $name;
Echo $name;
}

Demo2 ();//Cannot output normally
$name = "Linwu";
Manuscripts: Seven Stars interconnection www2.qixoo.com

PHP Local variables and global variables

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.