Sample code for using global as a global variable in php

Source: Internet
Author: User
Php programmers are familiar with the term process. Php engineers generally do not have a deep understanding of object-oriented, so they need to call external variables in methods in a large process, therefore, there will be many opportunities for using global variables. I will explain how to use global variables for the users in the input line, "global variables", the global word in this term tells us that this variable can be used everywhere. let's look at an example first:
The code is as follows:
$ A = 1;
$ B = 2;
Function Sum ()
{
Global $ a, $ B; // declare it as a global variable
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>

Result: 3
If no global variable is available, the values $ a and $ B cannot be obtained in the method. therefore, to use an external variable in the method, you must declare this variable as a global variable first, this is convenient.

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.