Variables and variable definitions in PHP _php tutorial

Source: Internet
Author: User
Tags http cookie parse error alphanumeric characters
A simple description of PHP variables and the definition of constants define common variables static variable pass address assignment (simple reference) PHP Super global variable $globals and other usages.

Variables are used to store values, such as numbers, text strings, or arrays.

Once a variable is set, we can use it repeatedly in the script.

All variables in PHP start with the $ symbol.

The correct way to set variables in PHP is:

$var _name = value; PHP beginners tend to forget the $ symbol in front of the variable. If you do that, the variable will be invalid.

Let's try to create a variable that holds a string, and a variable that has a value:

Example 1

The code is as follows Copy Code

$txt = "Hello world!";
$number = 16;
?>

Example 2

The code is as follows Copy Code

$foo = 25;
$bar = & $foo; Legal assignment
$bar = & (24 * 7); Illegal Referencing an expression without a name
function test ()
{
return 25;
}

$bar = &test (); Illegal
?>

Pre-defined variables
PHP provides a large number of predefined variables. Because many variables depend on the version and settings of the server that is running, and other factors, no detailed documentation is provided. Some of the predefined variables do not take effect when PHP is run as a command line.


Variable Range
The scope of a variable is the context range it defines (that is, its effective scope). Most PHP variables have a single range. This separate scope span also contains the files introduced by include and require.

PHP has a slightly different global variable from the C language, and in C, global variables are automatically applied in functions unless overridden by local variables. Local variables in PHP are not overwritten by global variables, and if used, they are the default initial values for variables. This can cause some problems, and some people may accidentally change a global variable. In PHP, global variables must be declared as globals when used in functions.

Using static variables
Another important feature of the variable range is the static variable (variable). A static variable exists only in the local function domain, but its value is not lost when the program executes away from the scope.


Precautions:
Naming rules for variables
The variable name must begin with a letter or underscore "_".
Variable names can contain only alphanumeric characters and underscores.
Variable names cannot contain spaces. If the variable name consists of multiple words, it should be delimited with an underscore (such as $my _string), or start with a capital letter (such as $myString).


PHP variable definition

1. Defining ConstantsDefine ("CONSTANT", "Hello World");
Constants can only contain scalar data (boolean,integer,float and string).
When calling a constant, simply use the name to get the value of the constant, not the "$" symbol, such as: Echo CONSTANT;
Note: Constants and (global) variables are in different namespaces. This means, for example, that TRUE and $TRUE are different.
2. Common Variables$a = "Hello";
3. Variable variable(Use two dollar sign ($))
$ $a = "world";
Each of the two variables is defined:
$a content is "hello" and $hello content is "world".
Therefore, it can be expressed as:
echo "$a ${$a}" or echo "$a $hello"; they all output: Hello World
To use mutable variables with arrays, you must address an ambiguous question. This is when you write the $ $a [1], the parser needs to know if you want to $a [1] as a variable, or you want $ $a as a variable and take out the value of the variable indexed to [1]. The syntax for solving this problem is to use ${$a [1] for the first case, and ${$a}[1 for the second case).
4. Static variables
Static $a inside the function = 0;
Note: Assigning a value to a declaration with the result of an expression results in a parse error such as static $a =3+3; (error)
Static variables exist only in the local function domain (inside the function), and after the function is executed, the value of the variable is not lost and can be used for recursive invocation
5. Global Variables
Global variables defined in the function body can be used outside the body, the global variables defined outside the function body cannot be used within the function body, and accessing variables globally can be customized $GLOBALS arrays with special PHP:
such as: $GLOBALS ["b"] = $GLOBALS ["a"] + $GLOBALS ["B"];
A real global variable imported in a function domain with the global statement actually establishes a reference to a global variable.
Global $obj;
Note: The static and global definitions for variables are implemented in an applied manner
6. Assigning Values to variables: Transfer address assignment (simple reference):
$bar = & $foo; Add & sign to the variable that will be assigned
Changing the new variable will affect the original variable, and this assignment is faster
Note: Only named variables can pass address assignment
Note: if
$bar = & $a;
$bar = & $foo;
Changing the value of $bar can only change the value of the variable foo without changing the value of a (the reference changed)
7.PHP Hyper global variable $globals: Contains a reference to a globally valid variable that refers to each current script. The array's key is labeled as the name of the global variable. $GLOBALS array exists starting with PHP 3.
$_server: The variable is set by the WEB server or directly associated with the execution environment of the current script. Similar to the old array $HTTP _server_vars array (still valid but opposed to use).
$_get: A variable that is submitted to the script via the HTTP GET method.
$_post: A variable that is submitted to the script via the HTTP POST method.
$_cookie: The variable that is submitted to the script via the HTTP cookie method.
$_files: A variable submitted to the script via an HTTP POST file upload.
File upload form to have enctype= "Multipart/form-data"
$_ENV: Executes the variables that the environment submits to the script.
$_request: The variable that is submitted to the script via the get,post and COOKIE mechanism, so the array is not trustworthy. The presence or absence of all variables contained in the array and the order of the variables are defined by the Variables_order configuration instructions in php.ini. The array does not directly emulate the earlier version of PHP 4.1.0. See Import_request_variables ().
Note: since PHP 4.3.0, the file information in $_files no longer exists in $_request.
$_session: The variable currently registered to the script session.

http://www.bkjia.com/PHPjc/629167.html www.bkjia.com true http://www.bkjia.com/PHPjc/629167.html techarticle a simple description of PHP variables and the definition of constants define common variables static variable pass address assignment (simple reference) PHP Super global variable $globals and other usages. Variables are used to store values, such as ...

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