Php Basics-example: use and difference of global and static

Source: Internet
Author: User
Tags php basics
Php Basics-instance: What is the use and difference between global and static ??? FunctiontestGloabl (){?????? Global $ testGloabl ;?????? $ TestGloabl = 2 ;?????? $ TestGloabl ++ ;????}???? T php Basics-instance: use and difference of global and static

?? ? Function testGloabl (){
??? ??? Global $ testGloabl;
??? ??? $ TestGloabl = 2;
??? ??? $ TestGloabl ++;
???? }
???? TestGloabl ();
???? Echo $ testGloabl. "|". $ GLOBALS ['testgloabl '];
????
???? Function testStatic (){
??? ??? Static $ testStatic = 2;
??? ??? $ TestStatic ++;
???? }

??? TestStatic ();
??? Echo $ testStatic; // if the variable uses the static keyword, $ testStatic is not defined during external function access;

?

Note:

1. use the global keyword. when the function where the keyword is located is called, it will be accessible anywhere in the current script.

2. use the static keyword. when the keyword is accessed in the function, it cannot be accessed outside the function.

In common: global? Static allows positioning variables to point to the same memory area.

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.