Global variable references in PHP

Source: Internet
Author: User

Global variables are defined outside the function, starting at the definition of the variable and at the end of the program file. However, unlike other languages, PHP's global variables are not automatically set to be available, in PHP the function can be considered as a separate program fragment, local variables will override the visibility of global variables, therefore, in the function cannot directly call global variables.

As follows:

<? PHP $one=100; $two=200;    // Defining global Variables funcation Demo () {    echo "operation Result:". ( $one+$two). " <br> ";    // The equivalent of a new declaration within the function of two function variables not assigned to the initial value, the result of the operation: 0 }demo ();? >

The correct way to refer is as follows:

<?php$one=100; $two =200;    Define Global Variables Funcation demo () {global $one, $two; echo "Operation Result:". ( $one + $two). " <br> ";    Operation Result: 300, using the global variable declared outside the function}demo (); >

Referencing the $global array of super-global variables

<?php$one=100; $two =200;    Define Global Variables Funcation demo () {  echo ' operation result: ". ( $GLOBAL [' One ']+ $GLOBAL [' $two ']). " <br> ";    Operation Result: 300, using the global variable declared outside the function}demo (); >

Global variable references in PHP

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.