Global and include problems in PHP

Source: Internet
Author: User
I have a single dish, and I don't know much about it in some areas of study. if I am not good at searching, I will send it to ask you. Thank you for your answers and help. Example: {code ...} a. php pseudocode: {code ...} b. php code: {code ...} c. php {code ...} goals to be achieved... I have a single dish, and I don't know much about it in some areas of study. if I am not good at searching, I will send it to ask you.
Thank you for your answers and help.

Example:

There are currently three files in a. php B. php c. php

The pseudocode a. php is as follows:


  

B. php code is as follows:

 

c.php

 

The goal is to directly read the variable value in the C. PHP file.

The general logic is as above. I personally think this is similar to assign/display in the framework, but I don't know how to implement it.

The above code reports an error:Notice: Undefined variable: str

I hope you can give me some advice (or write habits ).
Thank you.

Reply content:

I have a single dish, and I don't know much about it in some areas of study. if I am not good at searching, I will send it to ask you.
Thank you for your answers and help.

Example:

There are currently three files in a. php B. php c. php

The pseudocode a. php is as follows:


    

B. php code is as follows:

 

c.php

 

The goal is to directly read the variable value in the C. PHP file.

The general logic is as above. I personally think this is similar to assign/display in the framework, but I don't know how to implement it.

The above code reports an error:Notice: Undefined variable: str

I hope you can give me some advice (or write habits ).
Thank you.

global $array; Only modified the scope of $ array,extract($array);It is executed in the function, so the $ str scope is still inside the function. you can var_dump ($ arr ['str']) in c. php.

As for what you said, "I personally think this part is similar to the assign/display in the framework" and "but I don't know how to implement it", you don't know how to implement it, why do you think this is similar to assign/display in the framework?

In this example, the same is true if you use a class. you do not need to define three files separately and it looks comfortable. However, the functions you use indicate that you cannot perform object-oriented thinking well, the progress is still unstable. it makes no sense for us to tell you the sprint skills.

PHPAll the variables have scopes.assign/displayOther forms of template engines areextractOutput variables andincludeThe imported template file is placed in the same function. Only in this way can the variable scope be consistent and the value of the variable can be obtained from the introduced file. However, if you split two operations into different functions, you certainly cannot access each other. Let alonec.phpTo get the value of the variable, yousay("str","thinks");And cannot be obtained directly.

It seems that you have not really understood the scope of global and the internal variables of the function. We recommend that you check their concepts or usage again!
According to your ideas, $ str in var_dump ($ str); is the parameter 'str' in extract ($ array ', in fact, what you want var_dump to get at this time is the $ str variable in the say function.
This is nothing more:

function a(){    $str = '123';}a();var_dump($str);

You cannot get $ str.

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.