Global usage and global variables of php

Source: Internet
Author: User
: This article mainly introduces global usage and global variables of php. For more information about PHP tutorials, see. The global variables in php are different from those in other programming languages. in most programming languages, global variables automatically take effect in the functions and classes under them unless they are overwritten by local variables, or local variables with the same name and type cannot be declared at all, but the global variables in php do not take effect by default. In php, the Global variable must be declared as Global when used in a function. The Global keyword can be defined in the function. That is to say, Global is used to define Global variables. However, this Global variable is not applied to the entire website, but to the current page, including all files including include or require.

The following code uses an example to illustrate this problem:

 

It's easy to declare a global variable $ a = 5, then declare a function test () to print the global variable, then call the test (), according to the general programming language thinking, the output here must be 5. However, the actual execution is as follows:


The error is reported directly. it is simply unreasonable to say that a is not defined, but php is set like this, but I need to use $ a as a global variable, it is possible that there are many functions and many classes to use this $! This is. you need to declare it with the global keyword every time you use the global variable $.

The following code is a correct example:

 
The running result is as follows, so that a can be used. every time you use the global variable $ a, you must declare it like this. this is the php rule.

The following code is incorrect:

 
The running result is the same:


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the use of global and the global variables of php, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.