Home > Article > Backend Development > How to determine the type of a variable in php?
gettype() is used to get the type of variable. The returned type string may be one of the following strings: integer, double, string, array, object, unknown type
is_numeric (mixed var): //Check whether the measured variable is a number or digital character String
is_bool(): //Check whether the measured variable is of Boolean type
is_float(): //Check whether the measured variable is of floating-point type. Same usage as is_double and is_real()
is_int (): //Check whether the measured variable is an integer. Same usage as is_integer()
is_string(): //Check whether the measured variable is a string
is_object(): //Check whether the measured variable is a string Is an object
is_array(): //Check whether the measured variable is an array
is_null(): //Check whether the measured variable is empty
in PHP Type conversion
Type conversion refers to the conversion of a variable from one data type to another data type. The methods of type conversion are: There are two types, one is automatic conversion and the other is forced conversion.
AutomaticDiscrimination of type conversion
PHP does not require (or does not support) explicit types in variable definitions Definition; the variable type is determined by the context in which the variable is used. In other words, if a string value is assigned to the variable v a r ,
var, var, var becomes a string. If you assign an integer to $var, it becomes an integer.
<?php $var=123; var_dump($var); $var='hi'; var_dump($var); $var=true; var_dump($var);?>
The output is as follows:
#An example of PHP's automatic type conversion is the addition operator "+". If any operand is a float, all operands are treated as floats, and the result is also a float. Otherwise the operand is interpreted as an integer and the result is also an integer. Note that this does not change the types of the operands themselves; only how the operands are evaluated and the type of the expression itself is changed.
<?php //运算自动转换 $foo = "0"; // $foo 是字符串 (ASCII 48) var_dump($foo); $foo += 2; // $foo 现在是一个整数 (2) var_dump($foo); $foo = $foo + 1.3; // $foo 现在是一个浮点数 (3.3) var_dump($foo); $foo=1; $bar=$foo+1.22; //$foo还是一个整形,$bar是浮点数 var_dump($foo); var_dump($bar); ?>
The running results are as follows:
The above is the detailed content of How to determine the type of a variable in php?. For more information, please follow other related articles on the PHP Chinese website!