The variables that are in the include function are not callable

Source: Internet
Author: User
Tags variable scope
The variables included in the Include function are not callable.
1.php

echo $a;


2.php

$a = "AAAAAAAAAAAA";
Include ("1.php");



This writes the execution 2.php to be able to obtain the output correctly, but below can not
2.php

function LoadFile ($filename) {
Include $filename;
}
$a = "AAAAAAAAAAAA";
LoadFile ("1.php");


What is this for? Put the include into the function, and the file containing it cannot refer to its variables.
------Solution--------------------

function LoadFile () {
echo $a;
}
$a = "AAAAAAAAAAAA";
LoadFile ();


You'll find out if you try.
------Solution--------------------
Variable scope problem.

You have 1.php include inside the function, then variables declared outside the function cannot work on it. Unless you have a global look inside the function. Or use $globals[' a ' in 1.php instead of $ A.

In addition, I have a colleague who also likes to write a lot of papers, and then everywhere include,
I am very resistant to this practice, resulting in a global variable pollution, often I find a variable is where to declare or declare a variable is very laborious, afraid of causing conflict. And the code is messy.

It is therefore not advisable to use this approach. Try to control the variables in a local, not only for PHP performance benefits, code logic will be more clear.
------Solution--------------------
Well, this is a scope issue.
  • 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.