Invalid phpglobal variable

Source: Internet
Author: User
: 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.

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.