PHP Type Conversion Constant System Constant Magic Constant Detailed Explanation
- 2021-08-12 02:17:13
- OfStack
PHP Type Conversion, Constant, System Constant, Magic Constant Detailed Explanation
1. Automatic type conversion;
Automatic type conversion is performed during operation and judgment ;
1 ) Other types of conversion bool, Transition when judging ;
1 ) Integer to Boolean: 0 Turn false, Non 0 Convert to true ;
2) Empty string and ' 0' (" 0 ") Convert to false Other conversions true;
3) Empty array converted to false, Non-empty arrays are converted to true;
4) null Convert to false
5) Unsuccessful resource opening for false
Yes 0 Or empty, open unsuccessful turn to ' false','0';
2) Convert other types to strings ( String splicing );
null bool int float
1) null Convert to empty string ( Empty strings are not displayed )
2) Integer directly converted to corresponding string 5=>'5';
3) The real number is directly converted to the corresponding string, but the tail 0 Do not turn ( Last decimal point of real number 0);
4)true Convert to '1 ', false Convert to empty string ;
String, Boolean, Integer, Floating Point, null
3) Type conversion that occurs during operation
1)true/false Conversion 1/0;
2)null Convert to 0 ;
3) The value of the part at the beginning of the string is replaced by the corresponding value ;
4) If there is no numeric value at the beginning of the string, it 0 ;
( null,string,bool) < int < float
2. Force type conversion
Is to handle it manually 1 To another 1 Values of types ;
1 Type conversion function intval ,strval, floatval
$num1 = 1.2;
$num2 = strval($num1); // Right $num1 To a string ;
var_dump($num1);
var_dump($num2);
2 , ( Type )$ Variable Can't be converted resource
$num2 = (unset)$num2; Turn to null
3 , settype( Variable , Type ) Type must be written as a string ;
Change the type and value of a variable ;
Matters needing attention
1 ) Turn floating-point numbers into integers and directly kill decimals;
2) echo(int)($num1 + 0.5); 4 Shed 5 Into
3. Constants
1) Constant definition
define( Constant name, constant value )
define( ' SONG',22);
Attention :
1) The constant name is a string;
2) Constant value must be scalar
3) Constant name 1 Like capitalization, it is different from variables;
4) Constant names and variable names have the same naming rules ;
2) System constant
__LINE__ Current line number
__FILE__ The name of the current file
__DIR__ The directory of the current file;
PHP_OS Operating system
PHP_VERSION php Version of
Magic constant :
__FUNCTION__ Function name
__FUNCTION__ Function name ;
__CLASS__ Class name
__CLASS__ Class name
__METHOD__ Method name
__METHOD__ Method citizen
__NAMESPACE__ Name space
__NAMESPACE__ Name space ;
3 ) Constant judgment
defined( Constant name ) Constant name must be a string;
Determine whether a constant has been defined ;
4. Operators and expressions;
1 ) Arithmetic operator + - ( Multiplication ) /( Division ) % .
% Finding modulus / Remaining
0 % 3 = 0
12 % 10 = 2
5 % 19 = 4
1 % 3 = 1 ;
2 % 3 = 2 ;
3 % 3 = 0 ;
-9 % 4 = -1
9 % -4 = 1 ;
The sign of the result of modular operation depends on the first 1 Number.
Judge parity
X%2 == 0 Is even and divisible
X%2 != 0 Is odd and cannot be divisible
2 * 3 = 8
Exponential operation ;
2 3 = 8 ;
3 2 = 9;
If you have any questions, please leave a message or exchange and discuss in this community. Thank you for reading. I hope I can help you. Thank you for your support to this site!