Home  >  Article  >  Backend Development  >  Example of using php global variable global

Example of using php global variable global

WBOY
WBOYOriginal
2016-07-25 09:03:29863browse
  1. $a = 1;
  2. $b = 2;
  3. function Sum()
  4. {
  5. global $a, $b; //Declared as a global variable inside
  6. $b = $a + $b;
  7. }
  8. Sum();
  9. echo $b;
  10. ?>
Copy code

Result: 3 If there is no global variable global, the values ​​​​of $a and $b cannot be obtained within the method, so if you want to use external variables in the method, you need to declare the variable as a global variable first, and then you can use it.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn