Workaround for global variables globally invalidation after PHP multiple include _php tutorial

Source: Internet
Author: User

Workaround for global variables globally failure due to PHP multiple include


This article mainly introduces the solution of global variable's failure after multiple include of PHP, the example analyzes the principle and solution of $globals failure, and has certain reference value, the need of friends can refer to

This example describes a workaround that causes global variables to fail after multiple include in PHP. Share to everyone for your reference. The specific analysis is as follows:

In multiple files, the file is followed by an include, but the function in the last file cannot refer to the global variable after using global. For example:
a.php file:

?

1

2

3

$AA = 1;

?>

b.php file:

?

1

2

3

4

5

6

7

Include a.php

Function Show () {

Global $AA;

Var_dump ($AA);

}

?>

Display: null;

This failure is caused by a variety of reasons. An effective way is if you decide to use a variable as a global variable for multiple files, then using the $globals array is always valid. For example, the a.php in the above example:

?

1

2

3

$GLOBALS [' AA '] = 1;

?>

This variable can then be referenced in the functions and methods of multiple files.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/962406.html www.bkjia.com true http://www.bkjia.com/PHPjc/962406.html techarticle workaround for global variables globally invalidation after multiple include in PHP this article mainly introduces the solution of the global variable's invalidation after PHP multiple include, and the example analyzes the $globals ...

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