: This article mainly introduces that the phpglobal variable is invalid. if you are interested in the PHP Tutorial, refer to it. For beginners of PHP
globalKeyword, you may find that in the function
globalA variable outside a function cannot be correctly output in some cases (that is
globalInvalid variable ). The following is a simple and common example.
Here, we have two pages, a. php and B. php.
B. php page code:
A. php page code is as follows:
The above example is very simple. we hope that when we access the. php page, the welcome statements will be displayed correctly. However, when we access the. php page using a browser, we find that the output result is as follows:
Hello! Welcome to !
That is to say, in the functioninclude_view_page()CallingsayHi()Function Time, B. php pagesayHi()FunctionglobalOf$site_nameBut is not correctly identified and effective. What exactly is this?
In factinclude_view_page()Mediuminclude B. php page, B. php page variables$site_nameIt is stored ininclude_view_page()In the scope of the function. As we all knowglobalA variable is actually a reference to the global variables on the page in the function. In our example$site_nameFor a. php, the variable isinclude_view_page()The local variables in the function cannot beglobalThis variable, we naturally cannot get the correct variables and variable values when making the relevant calls.
In php, we need to pay special attentionincludeA page that changes the scope of variables on the page. To avoid this situation, we should minimize the number of levelsincludeAnd try not to use it in the function.include.
This failure is caused by multiple reasons. An effective solution is to always use the $ GLOBALS array:
$GLOBALS['site_name'] = 'CodePlayer';
Reference: http://www.365mini.com/page/php-global-invalid.htm
The above describes the invalid php global variables, including some content, and hope to be helpful to friends who are interested in PHP tutorials.