Php missing variable (1)

Source: Internet
Author: User
1. differences between common variables and global variables first, PHP is a LooselyTypedLanguage ). You do not need to declare the variable before setting it. $ Abc = & quot; 123 asdasdasd & quot; echo $ abc; echo & quot; & qu

1. differences between common variables and global variables

First, PHP is a Loosely Typed Language ).

You do not need to declare the variable before setting it.

 
$abc= "123asdasdasd";echo "
";echo $abc;echo "
";var_dump($abc);$abc = 234;echo "
";echo $abc;var_dump($abc);

The output, you know, is 123 and 234. variables are like a box. We put things in a box named abc. The box can only have one data type at the same time.

Question: The above $ abc = "123 asdasdasd"; isn't there two types of data: 123 and asdsdasd?

No, we use var_dump to print and find that it is of the string type, that is, the string type, because any variable content in php that is enclosed by single or double quotation marks will be treated as strings for processing.

The following $ abc = 123; is considered as a number.

During the development process, if you need to insert code in the middle, you should note that when naming, you should see that the previous code does not have this name, and it is useless later. If yes, the same name cannot be used.


About global

 

The output in the above example is 3.

What if I comment out global $ a and $ B above?

See the following example.

 

In this example, 2 is output.

Why?

This is because $ a = 1, $ B = 2; here, only local variables are declared, and methods in vivo and in vitro are two different scopes.

Therefore, $ B in the second example is irrelevant to the method body and can be considered as two different variables. So the result is 2. however, in the first example, after we declare $ a and $ B globally, the variables can be used in the method body and in vitro. the sum operation is $ a = 1, $ B = 2, so the final result is 3. in my opinion, using global to declare variables in the method body can also be seen as a process of passing values. Because if there is another function under sum that needs to be called $ a, $ B needs to be global again

If you do not understand the scope, you can look at the following example.

 ";echo $b;?>

5 and 2 are output here.

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.