PHP Learning Notes (iii): Data type conversions and constants introduction, Learning Note constants
One, PHP data types convert each other
1. Forced conversion
Copy the Code code as follows:
Bool,int,float,string,array,object,null
BOOL Settype (mixed $var, string $type)
1) Change the type of the original variable
Copy the Code code as follows:
$a = "123a"; String
Settype ($a, "int"); Do not omit double quotes
Var_dump ($a);
2) defined before assignment, does not change the original variable type
Copy the Code code as follows:
$a = "123ABC";
$b = (int) "123ABC";
Var_dump ($a);
Tips: Avoid memory overflow, integer 4 bytes (2.147E9), float 8 bytes
2. Automatic conversion
Automatically converted according to the running environment, such as the plus sign "+". If any one operand is a floating-point number, all operands are treated as floating-point numbers, and the result is a floating-point number.
Copy the Code code as follows:
$foo = "0″; $foo is a string (ASCII 48)
$foo + = 2; $foo is now an integer (2)
$foo = $foo + 1.3; $foo is now a floating point number (3.3)
$foo = 5 + "Ten Small Pigs"; $foo is an integer (15)
3. Variable test function
Copy the Code code as follows:
Is_bool (), Is_numeric (), Is_float (), Is_int () ...
Is_scalar ()//detect if the variable is a scalar
Second, the Declaration and use of constants
Copy the Code code as follows:
Cannot be changed after definition, can be accessed anywhere
Default case sensitive, used to capitalize
Constant values can only be scalar (int, float, string, bool)
Constants must be declared when the value is assigned
Define ("Constant name", "value")
Checks if a constant exists
Copy the Code code as follows:
bool Defined (string $name)
Var_dump (defined (' a ')); Do not omit the quotation marks
http://www.bkjia.com/PHPjc/985258.html www.bkjia.com true http://www.bkjia.com/PHPjc/985258.html techarticle PHP Learning Note (c): Data type conversion and constant introduction, learning Note constants One, PHP data types convert each other 1, the cast copy code code is as follows://Bool,int,floa ...