Introduction to include file variable scope in php _ PHP Tutorial

Source: Internet
Author: User
The scope of include file variables in php. Talking about the scope of include file variables in php This article summarizes the scopes in several cases when include files in php, which is very simple and practical, I hope you will be familiar with the use of include to talk about the scope of include file variables in php.

This article summarizes the scopes of the include file in php in several cases. it is very simple and practical, and I hope it will be helpful for you to be familiar with the use of include.

In php, we sometimes need to include a file. For example, when I was writing a framework some time ago, I planned to use the native php as the template and then write a display method to introduce the template file. but this is just my obscenity.

After writing the code, all the variables in the template are prompted to be undefined. The scope of the include file is summarized through various research and searching materials.

First case: file A includes file B. in file B, you can call the variable in file.

File A code:

?

1

2

3

4

$ Aaa = '000000 ';

Include "B. php ";

File B code:

?

1

2

3

Echo $ aaa;

The output content is normal.

The second case: file A includes file B, and then the variable of file B can be called in file.

File A code:

?

1

2

3

4

5

Include "B. php ";

Echo $ fff;

File B code:

?

1

2

3

$ Fff = 'I am f ';

At this time, the content can be output normally.

Case 3: Call file B in A method of A class in file A, and then call the variables in this method in file B.

File A code:

?

1

2

3

4

5

6

7

8

9

10

11

Class test {

Public function show (){

$ Bbb = 'abc ';

Include "B. php ";

}

}

$ T = new test;

$ T-> show ();

File B code:

?

1

2

3

Echo $ bbb;

At this time, the content can be output normally.

Case 4: File A Introduces file B through A defined function. in file B, variables in file A cannot be used, but variables in function (display) can be called in file.

File A code:

?

1

2

3

4

5

6

7

8

9

$ Aaa = '000000 ';

Function display ($ file ){

$ Bbb = 'asdasdas ';

Include $ file;

}

Display ("B. php ");

File B code:

?

1

2

3

Echo $ aaa;

Echo $ bbb;

After running $ aaa, the prompt is undefined. $ bbb can be output normally.

Therefore, it is not feasible for me to use a display method to introduce the template. Based on the three situations, I finally chose to write a class to import the template file. Currently, both ThinkPHP and Smarty use classes to introduce template files. If you have any shortcomings, please confirm them.

The above is all the content of this article. I hope you will like it.

This article summarizes the scopes of the include file in php in several cases. it is very simple and practical, and I hope to be familiar with the use of include...

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.