Workaround for global variables globally invalidation after PHP multiple include, global variable global_php tutorial

Source: Internet
Author: User

A workaround that causes global variables globally to fail after multiple include in PHP, global variables


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:

<?PHP$AA = 1;? >

b.php file:

<?phpinclude a.phpfunction 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:

<?php$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/962097.html www.bkjia.com true http://www.bkjia.com/PHPjc/962097.html techarticle Workaround for global variables globally invalidation after multiple include of PHP, global variables Globally This example describes a workaround that causes global variables to fail after multiple include in PHP. Share to ...

  • Related Article

    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.