diff --git a/language/types/type-juggling.xml b/language/types/type-juggling.xml index efad88d605..96c1565e38 100644 --- a/language/types/type-juggling.xml +++ b/language/types/type-juggling.xml @@ -1,65 +1,47 @@ - Type Juggling - PHP does not require (or support) explicit type definition in variable - declaration; a variable's type is determined by the context in which the - variable is used. That is to say, if a string value is assigned - to variable $var, $var becomes a - string. If an int value is then assigned to - $var, it becomes an int. + PHP does not require explicit type definition in variable declaration. + In this case, the type of a variable is determined by the value it stores. + That is to say, if a string is assigned to variable + $var, then $var is of type + string. If afterwards an int value is assigned + to $var, it will be of type int. - An example of PHP's automatic type conversion is the multiplication operator '*'. - If either operand is a float, then both operands are evaluated as - floats, and the result will be a float. Otherwise, - the operands will be interpreted as ints, and the result will - also be an int. Note that this does not - change the types of the operands themselves; the only change is in how the - operands are evaluated and what the type of the expression itself is. + PHP may attempt to convert the type of a value to another automatically + in certain contexts. The different contexts which exist are: + + + Numeric + + + String + + + Logical + + + Integral and string + + + Comparative + + + Function + + - - - -]]> - - - - - - If the last two examples above seem odd, see how - numeric strings - convert to integers. - + + + When a value needs to be interpreted as a different type, the value itself + does not change types. + + To force a variable to be evaluated as a certain type, see the section on @@ -67,119 +49,222 @@ examples: type of a variable, see the settype function. - - To test any of the examples in this section, use the - var_dump function. - + + Numeric contexts - - - The behaviour of an automatic conversion to array is currently - undefined. - + + This is the context when using an + arithmetical operator. + + + + In this context if either operand is a float (or not + interpretable as an int), both operands are interpreted as + floats, and the result will be a float. + Otherwise, the operands will be interpreted as ints, + and the result will also be an int. + As of PHP 8.0.0, if one of the operands cannot be interpreted a + TypeError is thrown. + + + + + String contexts + + + This is the context when using echo, + print, string interpolation, or the string + concatenation operator. + + + + In this context the value will be interpreted as string. + + + + + Logical contexts + + + This is the context when using conditional statements, the + ternary operator, + or a logical operator. + + + + In this context the value will be interpreted as bool. + + + + + Integral and string contexts + + + This is the context when using a + bitwise operators. + + + + In this context if all operands are of type string the result + will also be a string. + Otherwise, the operands will be interpreted as ints, + and the result will also be an int. + As of PHP 8.0.0, if one of the operands cannot be interpreted a + TypeError is thrown. + + + + + Comparative contexts + + + This is the context when using a + comparison operator. + + + + The type conversions which occur in this context are explained in the + Comparison with Various Types + table. + + + + + Function contexts + + + This is the context when a value is passed to a typed parameter, property, + or returned from a function which declares a return type. + - Also, because PHP supports indexing into strings via offsets - using the same syntax as array indexing, the following example - holds true for all PHP versions: + In this context, when coercive typing mode is active (the default), + only scalar values may be converted to another scalar value. + For simple types declarations the behaviour is as follows: + + + + bool type declaration: value is interpreted as bool. + + + int type declaration: value is interpreted as int + if conversion is well-defined. I.e. the string is + numeric. + + + float type declaration: value is interpreted as float + if conversion is well-defined. I.e. the string is + numeric. + + + string type declaration: value is interpreted as string. + + + + + If the type declaration is a union, see the section about + Coercive typing with union types. - - - -]]> - - - - - See the section titled String - access by character for more information. - - + + + + Internal functions automatically coerce &null; to scalar types, + this behaviour is DEPRECATED as of PHP 8.1.0. + + + Type Casting - - Type casting in PHP works much as it does in C: the name of the desired type - is written in parentheses before the variable which is to be cast. - + + Type casting converts the value to a chosen type by writing the type within + parentheses before the value to convert. + ]]> - + The casts allowed are: - + - - - (int), (integer) - cast to int - - - (bool), (boolean) - cast to bool - - - (float), (double), (real) - cast to float - - - (string) - cast to string - - - (array) - cast to array - - - (object) - cast to object - - - (unset) - cast to NULL - - + + (int) - cast to int + (bool) - cast to bool + (float) - cast to float + (string) - cast to string + (array) - cast to array + (object) - cast to object + (unset) - cast to NULL + - - (binary) casting and b prefix exists for forward support. Note that - the (binary) cast is essential the same as (string), but it should not be relied - upon. - + + + (integer) is an alias of the (int) cast. + (boolean) is an alias of the (bool) cast. + (binary) is an alias of the (string) cast. + (double) and (real) are aliases of + the (float) cast. + These casts do not use the canonical type name and are not recommended. + + - - The (unset) cast has been deprecated as of PHP 7.2.0. Note that the (unset) cast - is the same as assigning the value NULL to the variable or call. The - (unset) cast is removed as of PHP 8.0.0. - + + + The (real) cast alias has been deprecated as of PHP 8.0.0. + + - - Note that tabs and spaces are allowed inside the parentheses, so the - following are functionally equivalent: - - - - + + + The (unset) cast has been deprecated as of PHP 7.2.0. + Note that the (unset) cast is the same as assigning the + value NULL to the variable or call. + The (unset) cast is removed as of PHP 8.0.0. + + + + + + The (binary) cast and b prefix exists + for forward support. Currently (binary) and + (string) are identical, however this may change and + should not be relied upon. + + + + + + Whitespaces are ignored within the parentheses of a cast. + Therefore, the following are two casts are equivalent: + + ]]> - + + + + - + + Casting literal strings and variables to binary strings: - + + - + Instead of casting a variable to a string, it is also possible to enclose the variable in double quotes. - + @@ -218,60 +304,56 @@ if ($fst === $str) { It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections: + + Converting to boolean + Converting to integer + Converting to float + Converting to string + Converting to array + Converting to object + Converting to resource + Converting to NULL + The type comparison tables + - - - - - Converting to boolean - - - - - Converting to integer - - - - - Converting to float - - - - - Converting to string - - - - - Converting to array - - - - - Converting to object - - - - - Converting to - resource - - - - - Converting to NULL - - - - - The type comparison tables - - - - + + + + + + The behaviour of an automatic conversion to array is currently + undefined. + + + + + + Also, because PHP supports indexing into strings via offsets + using the same syntax as array indexing, the following example + holds true for all PHP versions: + + + + + +]]> + + + + + See the section titled String + access by character for more information. + + + + -