Static local variables of php

Source: Internet
Author: User
: This article mainly introduces the static local variables of php. if you are interested in the PHP Tutorial, please refer to them. Static local variables can be used. Global variablesIt is stored in the same area of the memory, but we leave the static local variable function and cannot access the variable. However, the variable is not destroyed, and its value is retained. When the function is called again, it can be accessed again. Note that:
1. when static local variables are declared, they cannot be Expressions. an error occurs in the expressions.

Static $ index = 1; static $ index = 5; // error

2. AssignmentStatic variablesExpressions are not allowed.

Static $ index = 1 + 1; // error

3. if we only declare a local variable that is not assigned a value, a 0 or empty string will be initialized for it by default. based on its type, determine whether it is a 0 or null string.

Example:
Dimensionality reduction of arrays (to one dimension)

ClassTest {publicfunctionreduce_arr ($ arr) {static $ temp = array (); // declares a static local variable foreach ($ arras $ key => $ val) {if (is_array ($ val) {$ this-> performance_arr ($ val);} else {$ temp [$ key] = $ val ;}} return $ temp ;}$ test = new Test (); $ arr = array ('0' => array ('good _ id' => 1 ), '1' => array ('good _ num' => 2), '2' => array ('good _ kids' => 2 )); var_dump ($ test-> performance_arr ($ arr); // A one-dimensional array is obtained.

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above describes the static local variables of php, including global variables and static variables. I hope my friends who are interested in the PHP Tutorial will be helpful.

    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.