php forced conversion types include: 1. Convert to integer type; 2. Convert to Boolean type; 3. Convert to floating point type; 4. Convert to string; 5. Convert to array; 6. Convert into object.
php coercion types are:
Type coercion in PHP is very similar to that in C: in The variable to be converted is preceded by the target type enclosed in parentheses.
The allowed casts are:
(int),(integer) - Convert to integer type
(bool ),(boolean) - Convert to Boolean type
(float),(double),(real) - Convert to floating point type
(string) through
1. Force conversion to Boolean value (bool)|(boolean)
Integer value 0 (zero)
Floating point type Value 0.0 (zero) Blank string and string "0" Array with no member variablesObject without cells (only for PHP 4)Special type NULL (including variables that have not been set)All other values are considered TRUE (including any resources).<?php var_dump((bool) ""); // bool(false) var_dump((bool) 1); // bool(true) var_dump((bool) -2); // bool(true) var_dump((bool) "foo"); // bool(true) var_dump((bool) 2.3e5); // bool(true) var_dump((bool) array(12)); // bool(true) var_dump((bool) array()); // bool(false) var_dump((bool) "false"); // bool(true) ?>
2. Force conversion to integer (int)|(integer)
To explicitly convert a value to integer, use (int) or (integer) Cast. In most cases, however, casting is not necessary because when an operator, function, or flow control requires an integer parameter, the value is automatically converted. You can also use the function intval() to convert a value to an integer type. a. Convert from boolb. Convert from floating point number, rounding, out of range, the result is uncertainc. Convert from string, see Convert string to numerical value
d. Convert from other types first to bool value, and then convert Never force an unknown fraction to an integer, as this may sometimes lead to unexpected results.<?php echo (int) ( (0.1+0.7) * 10 ); // 显示 7 ?> $str = "123.456abc7"; // (int)123 echo (int)$str; $str = "abc123.456"; // (int)0 $str = true; // (int)1 $str = false; // (int)0
3. Force conversion to floating point type (int)|(double)|(real)|doubleval()|floatval()|intval()
Precision: 0.12345678901234 // Both double and real are the same The missing parameter string of the data is converted into a numerical value4. Forced conversion to string (string) |strval()
You can use the (string) notation or thestrval()
function to convert a value to a string. When an expression requires a string, the conversion of the string is done automatically within the scope of the expression. For example, when using theecho()
orprint() function, or when comparing a variable value to a string.
The Boolean value TRUE will be converted to the string "1", while the value FALSE will be represented as "" (i.e. the empty string). This allows you to compare between booleans and strings at will.
When integer or floating-point values are converted into strings, the string consists of numeric characters representing these values (floating-point numbers also contain an exponent part).
print_r()
andYou can convert PHP values to strings to store them permanently. This method is called serialization and can be accomplished using the function serialize()
. If you set up WDDX support when you installed PHP, you can also serialize PHP values into XML structures.
5. Cast to array(array)
For any type: integer, floating point, string, Boolean and resource, if you convert a value For an array, you will get an array with only one element (its subscript is 0), which is the value of this scalar.
If an object is converted into an array, the elements of the resulting array are attributes (member variables) of the object, and their key names are member variable names.
If you convert a NULL value to an array, you will get an empty array.
6. Convert to object (object)
If you convert an object into an object, it will not change in any way.
If any other type of value is converted to an object, an instance of the built-in class stdClass will be created.
If the value is NULL, the new instance is empty. Converting an array to an object will cause the keys to become property names with corresponding values.
For any other value, the member variable named scalar will contain the value
7. Convert to resource (cannot be converted)
Due to Resource type variables hold special handles for open files, database connections, graphics canvas areas, etc., so other types of values cannot be converted into resources.
Note
HTML forms do not pass integers, floating point numbers or Boolean values, they only pass strings. To check whether a string is a number, you can use the is_numeric() function.
When the variable $x is not defined, usage such as if ($x) will cause an E_NOTICE level error. Therefore, you can consider using empty
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of What types of casts are there in php?. For more information, please follow other related articles on the PHP Chinese website!