Php variable access does not understand

Source: Internet
Author: User
Php variable access does not understand PHPcode & lt ;? Php $ my_var & quot; abc & quot; functionmy_fun () {echo $ my_var;} my_fun ();? & Gt; why can't I access the variable inside the function? the variable inside the function is the global variable outside the local variable function. it is reasonable to say that the variable outside the function can be accessed inside the function. Why can't I understand the function's php variable access? help
PHP code
  
  

Why can't variables outside the function be accessed inside the function?
The variable in the function is a local variable.
The global variables outside the function are supposed to be able to access the variables outside the function.
Why does the function prompt the local variable $ my_var may not have been initialized?

Thank you for your help!

------ Solution --------------------

Why cannot I access the variables outside the function? you can ask the global variables outside the function,
The variable in the function is a local variable.
The global variables outside the function are supposed to be accessible inside the function.
Why does the function prompt the local variable $ my_var may not have been initialized?

Thank you for your help!
$ My_var = "abc ";
Global $ my_var = "abc ";
------ Solution --------------------
Global $ my_var;
$ My_var = "abc ";

Discussion

Reference:
Why cannot I access the variables outside the function? you can ask the global variables outside the function,
The variable in the function is a local variable.
The global variables outside the function are supposed to be accessible inside the function.
Why does the function prompt the local variable $ my_var may not have been initialized?

Could you help me explain this ......

------ Solution --------------------
PHP code
  
------ Solution --------------------
As you can understand, there are two worlds inside and outside the function, and the time variables in both worlds are unknown by default. only the introduction gives you the opportunity to understand them. The Introduction method is global.

PHP code
    
------ Solution --------------------
To be set to a global variable,
------ Solution --------------------
I tested the code that was sent out after the test was successful.
Discussion

Reference:
PHP code
Global $ my_var;
$ My_var = 'abc ';

Function my_fun (){
Global $ my_var;
Echo $ my_var;
}
My_fun ();
?>
Global $ my_var in the second line; is it useless?

------ Solution --------------------
In front of the variable outside the function + global cannot afford to use
The variable outside the function is a global variable. + global is useless.
The global variable is added to the variables in the function. this design is for easy differentiation and understanding. Besides, the examples in this manual are very clear.
Http://www.php.net/manual/zh/language.variables.scope.php
------ Solution --------------------

------ Solution --------------------
$ A = 'abc ';
Function aaa (){
Echo $ a; // you can write $ GLOBALS ['A'] or global $ a on this statement;
}
?>

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.