(iii) PHP learning Note ―――― Basic syntax: variables

Source: Internet
Author: User
Creating variables

PHP does not have a command to create a variable, it is created when the first assignment is made, and is automatically converted to the correct data type.


PHP Naming conventions:

  • The variable starts with the $ sign, followed by the name of the variable.

  • The variable name must begin with an underscore and a letter.

  • Variable names can contain only alphabetic characters and underscores. (A-Z, 0-9, and _)

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


  • PHP variable Scope

  • Local (partial)

  • Global (globally)

  • Static (statically)


  • LOCAL Scope and global scope

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

    Variables declared inside a function have a local scope and can only be accessed inside the function.


    Give one of the two examples to improve your understanding.


     
      ";//Change the           echo $b;    The value is 2--there is no change, or what a person should do.           echo "
    "; } Test (); echo $a; Value is 1, unchanged---time flow, the goddess was Ko Fu handsome abandoned or a person echo "
    "; echo $b; Variable b in output function outside function, output is empty--goddess was abandoned back to find Loser,loser said when I was stupid, the baby in the belly is not mine, so the output is empty. echo "
    "; echo "The line above is empty!" "?>

    Global keywords

    In the case of the above example, it is to make loser (local variable B) The Magic Weapon of the goddess (global variable a)--to become a high-rich handsome

    Used both to access global variables within a function

      
     
      ";       echo  $b;  A value of 2--output variable b outside of the function is not empty--both now Gaofu and goddess of how to blind, how blind to engage in?>

    Static keyword

    In general, all local variables are deleted when the function is finished---both high and handsome, the goddess will lose

    However, sometimes I need not delete a local variable----also do not rule out happy life together

      
     
      

    You can get rid of the static to realize its role ...

  • 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.