PHP static statics Variable detailed

Source: Internet
Author: User
Tags count variables variable variable scope

PHP static statically variable

Another important feature of variable scope is static variable. A static variable exists only in a local function field, but its value is not lost when the program executes out of this scope. Take a look at the following example:

Examples demonstrating the need for static variables

 
  
  
  1. ? Php
  2. function Test ()
  3. {
  4. $w 3sky = 0;
  5. echo $w 3sky;
  6. $w 3sky++;
  7. }
  8. ?>

This function is not useful because the value of $w 3sky is set to 0 and output "0" each time it is invoked. Adding a variable to the $w 3sky++ does not work, because once the function is exited, the variable $w 3sky does not exist. To write a count function that does not lose the value of this count, define the variable $w 3sky as static:


Examples of using static variables

 
  
  
  1. ? Php
  2. function Test ()
  3. {
  4. static $w 3sky = 0;
  5. echo $w 3sky;
  6. $w 3sky++;
  7. }
  8. ?>


Now, each call to the Test () function will output $w 3sky value and add one.



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.