Introduction Why PHP Global variables are not effective _php tutorial

Source: Internet
Author: User
For a novice in the actual operation

But in the actual operation, will encounter a few times the global variable invalid depressed things, the following focus on the PHP global variables can not be effective reasons and solutions.

1.PHP global variable does not take effect error recurrence

The problem arises when using my simple framework, when you use the original third-party defined function in view (this function is more independent), the following simulation:

t1.php

    1. < ?
    2. Run (); Perform
    3. function Run () {
    4. Include ' func.php ';
    5. ShowGlobal ();
    6. }
    7. ?>

Func.php1

 
  
  
  1. < ?
  2. $ VARs ' I am global! ' ;
  3. function ShowGlobal () {
  4. Global $vars;
  5. Print (' I use global variables: '. $vars);
  6. }
  7. ?>

Very simple two files (the first time when the problem, far more complicated than this, after a layer of error, to get the least of the problem to reproduce the environment), func.php is a well-defined third-party function, the function uses some global variables, if you put these two files together, perform a t1.php, Will find the ShowGlobal in the $vars is not visible, global failure?

2.PHP global variable does not take effect error reason

After searching, it was found that some people had suggested it earlier, and some explanations were given: php.net
Originally, when included func.php in the run function of t1.php, the scope of the variable $vars in func.php was only within run, while in ShowGlobal using the global declaration of $ VARs is required to belong to t1.php is not a run function, so it is empty)

3.PHP global variable does not take effect workaround

Knowing the reason, the solution is very simple, you can remove the include from run, so that the $vars in the func.php is t1.php, or you can declare $vars in run with global, so you can also declare the $vars of the original run as the global (belonging to t1.php);

Although the problem can be simple, but it is still very uncomfortable to use, because in my phpec framework, the case of include is more common, it is not possible to move the view on-demand include to the outer layer, using global, when I use a third-party function, And don't want to know what global variables it uses, and, more hierarchies, ....

Summary of reasons why PHP global variables are not valid:

1) Minimize the multi-level and function in the include file.

2) Try not to use global variables


http://www.bkjia.com/PHPjc/446107.html www.bkjia.com true http://www.bkjia.com/PHPjc/446107.html techarticle for a novice in the actual operation, but in the actual operation, will encounter several times the global variable is not valid depressed thing, the following focus on the PHP global variable can not be effective reason and solve ...

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