PHP judges the value and type, how does PHP judge the type of a variable?

gettype() is used to get the type of the variable. The returned type string may be one of the following strings: integer, double, string, array, object, unknown type

is_numeric ( mixed var ): //Test whether the measurement variable is a number or a digital string

is_bool(): //Check whether the measurement variable is a boolean

is_float(): //Check whether the measurement variable is a floating point type and is_double, is_real() are the same usage

is_int(): //Check whether the measurement variable is the same usage as is_integer()

is_string(): //Check whether the measurement variable is a string

is_object(): //Check whether the measurement variable is an object

is_array(): //Check whether the measurement variable is an array

is_null(): //Check whether the measurement variable is empty

Type Conversion in PHP

Type conversion refers to the conversion of variables from one data type to another data type. There are two types of type conversion methods, one is automatic conversion, and the other is forced conversion.

PHP does not require (or does not support) explicit type definitions in variable definitions; variable types are determined based on the context in which the variable is used. That is, if you assign a string value to the variable var,

var, var, var becomes a string. If you assign another 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:

4adc0d8ff1132775480f008ac2cedde8.png

An example of PHP's automatic type conversion is the addition operator "+". If any of the operands are floats, 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; what changes is only how the operands are evaluated and the types of the expressions themselves. <?php //The operation automatically converts $foo = "0"; // $foo is a string (ASCII 48)

var_dump($foo); $foo += 2; // $foo is now an integer (2)

var_dump($foo); $foo = $foo + 1.3; // $foo is now a float (3.3)

var_dump($foo); $foo=1; $bar=$foo+1.22; //$foo is still an integer, $bar is a float

var_dump($foo);

var_dump($bar);

?>

The results are as follows:

55cbb2ceec6225d36b6cbd0247363b6e.png

Related: PHP judges the value and type, how does PHP judge the type of a variable?