Sample code Demo for PHP static variable static

Source: Internet
Author: User
Sample code for PHP static variable static demo

In the range of variables in PHP, there is a very important variable ―php static variable statics. In the following article, we will use specific code examples for you to detail the main usage of static variables in PHP, we hope to help you.

Take a look at the following example:

Example of a static variable that requires PHP

<? PHP function Test () {$w 3sky = 0; echo $w 3sky; $w 3sky++;}?>

This function is of little use because the value of $w 3sky is set to 0 and output "0" each time it is called. Adding a variable to a $w 3sky++ has no effect, because once you exit this function, the variable $w 3sky does not exist. To write a count function that does not lose this count value, define the variable $w 3sky as static:

Example using PHP static variable statics

<? PHP function Test () {static $w 3sky = 0; echo $w 3sky; $w 3sky++;}?>

Now, each call to the Test () function outputs the value of $w 3sky and adds one.

  • Related Article

    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.