(3) PHP study Notes--Basic syntax: variables-php Tutorial

Source: Internet
Author: User
(3) PHP study Notes-basic syntax: variable creation

PHP does not have a command to create variables. it is created when the first value is assigned and automatically converted to the correct data type.


PHP naming rules:

  • A variable starts with $, followed by the variable name.

  • The variable name must start with an underscore (_) and a letter.

  • Variable names can only contain letters and underscores. (A-z, 0-9, and _)

  • Variable names are case sensitive ($ S and $ s are not the same variable)


  • PHP variable scope

  • Local)

  • Global)

  • Static)


  • LOCAL scope and Global scope

    Variables declared outside the function have a Global scope and can only be accessed outside the function.

    Variables declared in the function have LOCAL scopes and can only be accessed within the function.


    Let's give a second example to increase your understanding.


     "; // Line feed echo $ B; // The value is 2-no change, or what should a person do. Echo"
    ";}Test (); echo $ a; // The value is 1, unchanged --- time flow, the goddess was abandoned by Gao fushuai or a person echo"
    "; Echo $ B; // variable B in the external output function of the function. the output is empty. The goddess is discarded and comes back to loser, loser said that when I was dumb, the children in my stomach were not mine, so the output was empty. Echo"
    "; Echo" the above line is empty! "?>

    Global keywords

    In combination with the example above, it is used to make loser (local variable B) a magic weapon to attack the goddess (global variable a)-become Gao fushuai

    Used to access global variables in a function

      
     "; Echo $ B; // The value is 2 -- the output variable B outside the function is not empty. -- how can we make it if Gao fushuai and goddess want to?>

    Static keywords

    Generally speaking, after the function is executed, all local variables will be deleted-the goddess will get rid of Gao fushuai.

    However, sometimes I do not need to delete a local variable-or even make it easier to live together.

      
     

    You can try it out of static...

    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.