Table of Contents [1] Variable variable definition variable assignment [2] data type string integer floating-point Boolean Array object null resource [3] scope globalstatic
Variable
Variable definition
A variable is a container for storing information, beginning with a $ symbol, followed by a variable name. Variable names must start with a letter or underscore and are case sensitive
Assigning values to variables
PHP does not have a command to create a variable, and the variable is created when it is first assigned a value
Data type
PHP is a loosely typed language, and without having to tell the PHP variable the data type, PHP automatically converts the variable to the correct data type based on its value. There are 8 types of PHP Data: Four scalar types, two composite types, and two special types. This is: string, Integer, floating-point, Boolean, Array, object, NULL, resource
[Note]var_dump () returns the data type and value of the variable
String
The string can be any text within quotation marks, which can be single or double quotation marks
"; $x = ' Hello world! '; /hello World!echo $x;? >
<情况1> When the string contains quotation marks, there are three solutions:
[1] double quotes embedded in single quotation marks
[2] Single quotation marks embedded in double quotation marks
[3] using the escape character "\"
"Echo $str _string2;echo"
"Echo $str _string3; >
<情况2> When the quotation marks of a string encounter a variable, there are two cases:
[1] When a variable is enclosed in double quotes, the variable is concatenated with the contents of the double quotation mark
[2] When the single quotation mark contains a variable, the variable is treated as a string output
"Echo $str _string2;//1echo"
"Echo $str _string3;//" $test "echo"
"; Echo $str _string4;//' 1 '?>
<情况3> When a string is long, a method using the Heredoc structure is used, first the delimiter represents the string (<<<), followed by an identifier (any name) after (<<<), a string after wrapping, and the end of the string with this identifier. Note The identifiers do not have extra spaces around
Integer
PHP integers must have at least one number, not a comma or a space, no decimal points, plus or minus, you can specify integers in three formats: decimal, hexadecimal (prefix is 0x) or octal (prefix is 0)
"; $x =-345; Int ( -345) var_dump ($x); echo "
"; $x = 0x11; Int (var_dump) ($x); echo "
"; $x = 011; Int (9) var_dump ($x);? >
Floating point number
PHP floating point number is a decimal or exponential form of numbers
"; $x = 2.4e3;//float (2400) var_dump ($x); echo "
"; $x = 8e-1;//float (0.8) Var_dump ($x);? >
Boolean type
PHP Boolean has only two values: TRUE or false (case insensitive), often used for conditional testing. When you output a Boolean type with the echo command, output "1" if true, false to output nothing
" ; $flag = $man = = "female";//Do not output echo $flag; Var_dump ($flag);//bool (false)?>
Array
PHP arrays can store multiple values in a variable
String (5) "Volvo" [1]=> string (3) "BMW" [2]=> string (4) "SAAB"}var_dump ($cars);? >
Object
A PHP object is a data type that stores data and information about how the data is processed. In PHP, you must explicitly declare an object, but first you must declare the class of the object. For this, using the class keyword, classes are structures that contain properties and methods. Then define the data type in the object class, and then use this data type in an instance of the class
color = $color; } function What_color () { return $this->color; }} function Print_vars ($obj) { foreach (Get_object_vars ($obj) as $prop = = $val) { echo "\t$prop = $val \ n"; }} $herbie = new Car ("white"), echo "\herbie:properties\n";p rint_vars ($herbie);? >
Null
Null in PHP is a null type, insensitive to case, NULL type has only one value, indicating that a variable has no value, when it is assigned to NULL, or has not been assigned, or is unset (), the variables are considered null in three cases
Resources
PHP resources are created and used by specialized functions, such as open files, data connections, and graphical canvases. You can manipulate resources (create, use, and release). Any resources should be released in a timely manner when they are not needed. If we forget to release the resources, the system automatically enables the garbage collection mechanism to recycle the resources after the page has been executed to avoid the memory being exhausted
"; NewLine }}fclose ($file _handle);//Close File?>
[Note]memory_get_usage () Gets the memory consumed by the current PHP unit in byte
"; $var _string = ' 123 ';//320echo $m 2 = memory_get_usage ()-$m 1; echo "
"; $n = 123;//272echo $m 3 = memory_get_usage ()-$m 1-$m 2; echo "
"; $f = 123.00;//272echo $m 4 = memory_get_usage ()-$m 1-$m 2-$m 3; echo "
"; $var _array = Array (' 123 ');//576echo $m 5 = memory_get_usage ()-$m 1-$m 2-$m 3-$m 4;?>
"; $string = 9494; Var_dump ($string);//int (9494) echo "
";? >
Scope
PHP has three different variable scopes: local (local), Global, static
Variables declared outside the function have global scope and can only be accessed outside of the function; The variables declared inside the function have a local scope and can only be accessed inside the function.
To test the variables inside the function:"; echo "Variable x is: $x
"; echo "Variable y is: $y";} No output//10mytest (); echo "
To test variables outside of a function:
The "; echo" variable x is: $x
";//5echo" Variable y is: $y ";//No Output?>
Global keywords
Used to access global variables within a function
PHP also stores all global variables in an array named Globals[index], and the subscript contains the variable name. This array is also accessible within the function and can be used to update global variables directly
Static keyword
In general, when a function is completed or executed, all variables are deleted, but sometimes a local variable needs not be deleted. To do this, you need to use the static keyword when declaring the variable for the first time. Whenever a function is called, the information stored by the variable is the information contained in the last Call of the function, but it is important to note that the variable is still a local variable of the function.
"; MyTest ();//1echo"
"; MyTest ();//2echo"
"; MyTest ();//3echo"
"; MyTest ();//4?>