Basic data types supported by php:
Integer: Integer
Float (Double, Double): floating point, indicating all real numbers
String: String
Boolean: Boolean, true/false
Array: stores multiple data items of the same type.
Object: Object, the instance that saves the class
NULL: a variable that is not assigned a value, has been reset, or has been assigned a special value of NULL.
A specific built-in function (such as a database function) will return a resource-type variable.
Four scalar types:
String (string)
Integer)
Float (float, also double)
Boolean (boolean)
Two composite types:
Array)
Object)
Two special types:
Resource)
NULL)
View variable types
The gettype () function allows you to conveniently view the type of a variable:
The code is as follows: |
Copy code |
<? Php $ Var_bool = TRUE; // a boolean $ Var_str = "foo"; // a string $ Var_int = 12; // an integer Echo gettype ($ var_bool); // output boolean Echo gettype ($ var_str); // output string Echo gettype ($ var_int); // output integer ?> |
Prompt
For historical reasons, for float data, the gettype () function returns double instead of float.
To view the value and type of an expression, use the var_dump () function.
Judge variable type
To determine the next logical action by determining the variable type, use the is_type series functions instead of gettype:
The code is as follows: |
Copy code |
<? Php $ Var_int = 12; // If $ var_int is of the int type, add If (is_int ($ var_int )){ $ Var_int = $ var_int + 4; } Echo $ var_int; // output 16 ?> |
Both NULL and resource represent external resources, such as database connections.
Basically, a resource variable cannot be operated directly, but they are usually returned by the function and must be passed as parameters to other functions.
In php, the variable type is determined by the value assigned to the variable. (Dynamic language)
PHP can determine the type of a variable based on the value stored in the variable at any time, that is, it can transparently change the type of the variable at any time.
PHP automatically retrieves the input data type. Once a variable value is retrieved from the variable, it returns data of the same data type.
Habit: constant names are composed of uppercase letters. This makes it easy to distinguish between variables and constants.
A major difference between a constant and a variable is that when a constant is referenced, there is no $ symbol before it.
Another difference between constants and variables is that constants can only store Boolean, integer, floating point, or string data. These types are scalar data.
Variable type change (juggling)
PHP does not need (or does not support) to specify the variable type in the name variable; the type of a variable is determined by the relationship between the variables used, that is, if you assign a string value to a variable var, var becomes a string variable. If you assign an integer to var, it becomes an integer variable. An example of PHP's automatic conversion of variable types is the addition operator '+ '. If any operand is a double-precision number, all operands are evaluated as double-precision numbers, and the result is also a double-precision number. Otherwise, the operand is considered an integer, and the result is also an integer. Note that this does not affect the variable type of each operand. The only change is how the operand is processed during calculation. $ Foo = "0"; // $ foo is a string with a value of "0" (ASCII 48)
PHP code
The code is as follows: |
Copy code |
$ Foo = "0"; // $ foo is a string with a value of "0" (ASCII 48) $ Foo ++; // $ foo is a string with the value "1" (ASCII 49) $ Foo + = 1; // $ foo is now an integer (2) $ Foo = $ foo + 1.3; // $ foo is a number of double precision (3.3). $ Foo = 5 + "10 Little Piggies"; // $ foo is an integer (15) $ Foo = 5 + "10 Small Pigs"; // $ foo is an integer (15) |
If you think the last two expressions in the above example seem a bit strange, please refer to the "string conversion" section. If you want to force a variable to be calculated as a fixed type, see the "casting" section. If you want to change the type of a variable, see the description of the function "settype.
Determine the type of a variable
Because PHP decides the type of variables and converts them as needed, the type of a specific variable is not very obvious at any time. PHP includes some functions to find out the type of this variable. These functions are gettype (), is_long (), is_double (), is_string (), is_array (), and is_object ().
Type casting)
In PHP, the type is forced to be roughly the same as in C: write the required type in parentheses before the variable to be forced.
PHP code
The code is as follows: |
Copy code |
$ Foo = 10; // $ foo is an integer. $ Bar = (double) $ foo; // $ bar is a double number. |
The following mandatory methods are allowed: (int), (integer)-forced to be an integer (real), (double), (float)-forced to be a double precision (string) -force to string (array)-Force to array (object)-Force to object. Note that in parentheses, tabs (tabs) and spaces (spaces) are allowed ), therefore, the following statement is equivalent: $ foo = (int) $ bar;
String conversion
When a string is calculated as a numeric value, its results and types are determined as described below. If the string contains characters '.', 'e', or 'e', it is treated as a double-precision variable. Otherwise, it is treated as an integer. The value of this string is determined by the first part of the word. If the string starts with any valid numeric data, the numeric data is the value of this string. Otherwise, the value is zero ). Valid numeric data follows the following tags, followed by one or more numbers (which can contain decimal points), followed by an optional index. An index consists of one or more numbers following 'E' or 'e.
PHP code
The code is as follows: |
Copy code |
$ Foo = 1 + "10.5"; // $ foo is a double precision (11.5) $ Foo = 1 + "-1.3e3"; // $ foo is a double precision (-1299) $ Foo = 1 + "bob-1.3e3"; // $ foo is an integer (1) $ Foo = 1 + "bob3"; // $ foo is an integer (1) $ Foo = 1 + "10 Small Pigs"; // $ foo is an integer (11) $ Foo = 1 + "10 Little Piggies"; // $ foo is an integer (11 ); // This string contains the character 'e' |
Identifier
Operator:
1) value assignment operator: =
2) arithmetic operators: +,-, *,/, % (modulo)
3) join operator:. No matter what the operand is, it is treated as a String and the result returns a String.
4) Total value Assignment operator (Combined Assignment Operators): + =, * =,/=,-=, % =,. =
5) auto increment/Decrementing operator (Automatically Incrementing and Decrementing ):
(1) $ variable + = 1 & hArr; $ variable ++; $ variable-= 1 & hArr; $ variable-. Perform other operations like C, plus ++ or-
(2) ++ $ variable,-$ variable, first ++ or-, and then perform other operations
6) comparison operators:
= (Left equals to right ),! = (Left is not equal to right ),
===( The left side is equal to the right side, and the data type is the same ),
>=, >,<, <=
7) logical operators: | or, & and, xor (returns true if only one of the left and right sides is true ),!
A reference table
Arithmetic Operators)
Operator Symbol |
Description Description |
Example Case |
Result Result |
+ |
Addition Plus sign |
X = 2 X + 2 |
4 |
- |
Subtraction Minus sign |
X = 2 5-x |
3 |
* |
Multiplication Multiplication number |
X = 4 X * 5 |
20 |
/ |
Division Division number |
15/5 5/2 |
3 2.5 |
% |
Modulus (division remainder) Modulus (remainder) |
5% 2 10% 8 10% 2 |
1 2 0 |
++ |
Increment Auto-increment |
X = 5 X ++ |
X = 6 |
-- |
Decrement Auto-subtraction |
X = 5 X -- |
X = 4 |
Assignment Operators
Assignment Operators)
Operator Symbol |
Example Case |
Is The Same Equivalent |
= |
X = y |
X = y |
+ = |
X + = y |
X = x + y |
-= |
X-= y |
X = x-y |
* = |
X * = y |
X = x * y |
/= |
X/= y |
X = x/y |
% = |
X % = y |
X = x % y |
Comparison Operators
Comparison Operators)
Operator Symbol |
Description Description |
Example Case |
= |
Is equal Equal |
5 = 8 return false |
! = |
Is not equal Not equal |
5! = 8 return true |
> |
Is greater Greater |
5> 8 return false |
< |
Is less Less |
5 <8 return true |
> = |
Is greater than or equal Greater than or equal |
5> = 8 return false |
<= |
Is less than or equal Less than or equal |
5 <= 8 return true |
Logical Operators
Logical Operators)
Operator Symbol |
Description Description |
Example Case |
&& |
And And |
X = 6 Y = 3(X <10 & y> 1) returns true. |
| |
Or Or |
X = 6 Y = 3(X = 5 | y = 5) false |
! |
Not Non |
X = 6 Y = 3! (X = y) returns true. |
Other operators:
Ternary operators, error suppression operators, execution operators, array operators, type operators
Operator "? "Previous expression
If an expression containing binary operators appears in the ternary operator "? "? "Before, you should add a pair of parentheses to the expression. For example:
(X> = 0 )? X:-x;
Next let's look at a simple one? Number expression instance
The code is as follows: |
Copy code |
<? $ A = 1; Echo $? 'True': 'false '; The output result is trur; Check again If ($) { Echo 'true '; } Else { Echo 'false '; } |