Summary of PHP variables

Source: Internet
Author: User
PHP variables always start with the dollar sign $, followed by the variable name. Variable names follow the naming rules of identifiers, that is, a variable can start with a letter or underline, consisting of letters, underscores, numbers, and other ASCII characters from 127-255. Note that

PHP variables always start with the dollar sign $, followed by the variable name. Variable names follow the naming rules of identifiers, that is, a variable can start with a letter or underline, consisting of letters, underscores, numbers, and other ASCII characters from 127-255. Note that variables are case sensitive.

PHP does not need to display declaration variables. variable declaration can be performed at the same time as assignment.Good programming habits: all variables should be declared before use, preferably with annotations. 1. assign values to variables after they are declared. There are two ways to assign values: value assignment and reference assignment. 1. value assignment $ color = "red"; $ sum = 12 + "15 "; /* $ sum = 27 */2. if you want two variables to point to the same copy of a value, you need to assign values through reference. The variables created by referencing the assignment are the same as those referenced by another variable. if multiple variables reference the same content and modify any of the variables, the other variables will be reflected. Example: 2. variables can be declared at any position of the PHP script in the scope of the variables, but the location of the declared variables will greatly affect the scope of the access variables. The accessible scope is called scope ). Scope in PHP variable 4: △local variable △function parameter △global variable △static variable 1. variables declared in the function are considered local variables and can only be referenced in the function, when you exit a function that declares a variable, the variable and the corresponding value are revoked. This eliminates the possibility that variables that cause global access are intentionally or unintentionally modified. $ X = 4; function assignx () {$ x = 0; print "\ $ x inside function is $ x.
";} Assignx (); print" \ $ x outside of function is $ x.
"; The code execution result is: $ x inside function is 0. $ x outside function is 4. 2. function parameters like other programming languages, any function that accepts parameters must declare these parameters in the function header. Although these parameters accept values outside the function, they cannot be accessed after exiting the function. (Except for parameters passed by reference) for example: function x10 ($ value) {$ value = $ value * 10; return $ value;} after the function is executed, the parameter is about to be revoked. 3. global variables are opposite to local variables. global variables can be accessed anywhere in the program. When changing a GLOBAL variable in a function, you need to display the life of the variable as a GLOBAL variable in this function, as long as you add GLOBAL before the variable in the function. Example: $ somevar = 15; function addit () {GLOBAL $ somevar; $ somevar ++; print "somevar is $ somevar" ;}addit (); $ somevar: The value displayed is 16. However, if GLOBAL $ somevar is removed, the variable $ somevar is implicitly set to 0 and 1 is added. The value displayed at the end is 1. Another way to declare GLOBAL variables is to use the $ GLOBAL array of PHP, as shown below: $ somevar = 15; function addit () {$ GLOBALS ['somevar '] ++ ;} addit (); print "somevar is ". $ GLOBALS ['somevar ']; return value: somevar is 16. 4. static variable static scope. Function parameters of common variables are revoked at the end of the function, but static variables do not lose the value when the function exits, and the value can be retained when the function is called again. You can declare a STATIC variable by adding the keyword STATIC before the worker beam. STATIC $ somevar; consider an example: function keep_track () {STATIC $ count = 0; $ count ++; print $ count; print"
";} Keep_track (); if $ count is not specified as static (corresponding, $ count is a local variable ), the output will be 1 1 1 1 because $ count is static, it will retain the previous value each time the function is executed. The output is as follows: 1 2 3 4 static scope is useful for recursive functions. A recursive function is a powerful programming concept. it is a function that can call itself repeatedly until a certain condition is met. 5. PHP's Super global variable PHP provides many useful pre-defined variables that can be accessed at the person and location where the script is executed and used to provide a large amount of environment-related information. You can use these variables to obtain detailed information about the current user session, user operating system environment, and local operating environment. PHP creates some variables, while the availability and value of many other variables depend on the operating system and WEB services. Output all predefined variables: foreach ($ _ SERVER as $ var =>$ value) {echo "$ var =>$ value
";} Display the user's IP address: print" HI! Your IP address is ". $ _ SERVER ['remote_addr ']; to use a pre-defined variable array in PHP, you must enable the configuration parameter track_vars in the PHP. ini file.

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.