Type Juggling
Type Juggling
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 is assigned to variable
, then is of type
. If afterwards an value is assigned
to , it will be of type .
string``$var``$var``string``int``$var``int
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
Opmerking: > When a value needs to be interpreted as a different type, the value itself does change types. not
To force a variable to be evaluated as a certain type, see the section on
. To change the
type of a variable, see the function.
Type castingsettype
Numeric contexts
This is the context when using an . arithmetical operator
In this context if either operand is a (or not
interpretable as an ), both operands are interpreted as
s, and the result will be a .
Otherwise, the operands will be interpreted as s,
and the result will also be an .
As of PHP 8.0.0, if one of the operands cannot be interpreted a
is thrown.
float``int``float``float``int``int``TypeError
String contexts
This is the context when using ,
,
,
or the string
.
echo``printstring interpolationconcatenation operator
In this context the value will be interpreted as .
If the value cannot be interpreted a is thrown.
Prior to PHP 7.4.0, an was raised.
string``TypeError``E_RECOVERABLE_ERROR
Logical contexts
This is the context when using conditional statements, the , or a . ternary operatorlogical operator
In this context the value will be interpreted as .
bool
Integral and string contexts
This is the context when using . bitwise operators
In this context if all operands are of type the result
will also be a .
Otherwise, the operands will be interpreted as s,
and the result will also be an .
As of PHP 8.0.0, if one of the operands cannot be interpreted a
is thrown.
string``string``int``int``TypeError
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.
In this context the value must be a value of the type. Two exceptions exist, the first one is: if the value is of type and the declared type is , then the integer is converted to a floating point number. The second one is: if the declared type is a
type, the value is convertable to a scalar type,
and the coercive typing mode is active
(the default), the value may be converted to an accepted scalar value.
See below for a description of this behaviour.
int``floatscalar
Waarschuwing: > automatically coerce null to scalar types, this behaviour is as of PHP 8.1.0. Internal functionsDEPRECATED
Coercive typing with simple type declarations
- type declaration: value is interpreted as .
bool``bool - type declaration: value is interpreted as if the conversion is well-defined. For example the string is .
int``intnumeric - type declaration: value is interpreted as if the conversion is well-defined. For example the string is .
float``floatnumeric - type declaration: value is interpreted as .
string``string
Coercive typing with union types
When is not enabled, scalar type declarations
are subject to limited implicit type coercions.
If the exact type of the value is not part of the union, then the target type
is chosen in the following order of preference:
If the type exists in the union and the value can be coerced to the
type under PHP's existing type-checking semantics, then the type is chosen.
Otherwise, the next type is tried.
strict_types1. int
2. float
3. string
4. bool
Let op: > As an exception, if the value is a string and both int and float are part of the union, the preferred type is determined by the existing
semantics. For example, for is chosen, while for is chosen. numeric string`"42"``int``"42.0"``float`
Opmerking: > Types that are not part of the above preference list are not eligible targets for implicit coercion. In particular no implicit coercions to the , , and types occur.
null``false``true
Voorbeeld: Example of types being coerced into a type part of the union
<?php
// int|string
42 --> 42 // exact type
"42" --> "42" // exact type
new ObjectWithToString --> "Result of __toString()"
// object never compatible with int, fall back to string
42.0 --> 42 // float compatible with int
42.1 --> 42 // float compatible with int
1e100 --> "1.0E+100" // float too large for int type, fall back to string
INF --> "INF" // float too large for int type, fall back to string
true --> 1 // bool compatible with int
[] --> TypeError // array not compatible with int or string
// int|float|bool
"45" --> 45 // int numeric string
"45.0" --> 45.0 // float numeric string
"45X" --> true // not numeric string, fall back to bool
"" --> false // not numeric string, fall back to bool
"X" --> true // not numeric string, fall back to bool
[] --> TypeError // array not compatible with int, float or bool
?>
Type Casting
Type casting converts the value to a chosen type by writing the type within parentheses before the value to convert.
Voorbeeld: Type Casting
<?php
$foo = 10; // $foo is an integer
$bar = (bool) $foo; // $bar is a boolean
var_dump($bar);
?>
The casts allowed are:
(int)``int``(bool)``bool``(float)``float``(string)``string``(array)``array``(object)``object``(unset)``NULL> Waarschuwing: > is an alias of the cast.
is an alias of the cast. is an alias of the cast. and are aliases of the cast. These casts do not use the canonical type name and are deprecated as of PHP 8.5.0.
(integer)``(int)``(boolean)``(bool)``(binary)``(string)``(double)``(real)``(float)
Waarschuwing: > The cast alias has been deprecated as of PHP 7.4.0 and removed as of PHP 8.0.0.
(real)
Waarschuwing: > The cast has been deprecated as of PHP 7.2.0. Note that the cast is the same as assigning the value to the variable or call. The cast is removed as of PHP 8.0.0.
(unset)``(unset)``NULL``(unset)
Let op: > The cast and prefix exists for forward support. Currently and are identical, however this may change and should not be relied upon.
(binary)``b``(binary)``(string)
Opmerking: > Whitespaces are ignored within the parentheses of a cast. Therefore, the following two casts are equivalent:
<?php $foo = (int) $bar; $foo = ( int ) $bar; ?>
Casting literal s and variables to binary
s:
string``string
<?php
$binary = (binary) $string;
$binary = b"binary string";
?>
Instead of casting a variable to a , it is also possible
to enclose the variable in double quotes.
string
Voorbeeld: Different Casting Mechanisms
<?php
$foo = 10; // $foo is an integer
$str = "$foo"; // $str is a string
$fst = (string) $foo; // $fst is also a string
// This prints out that "they are the same"
if ($fst === $str) {
echo "they are the same", PHP_EOL;
}
?>
It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections:
Converting to booleanConverting to integerConverting to floatConverting to stringConverting to arrayConverting to objectConverting to resourceConverting to NULLThe type comparison tables
Opmerking: > Because PHP supports indexing into s via offsets using the same syntax as indexing, the following example holds true for all PHP versions:
string``arrayVoorbeeld: Using Array Offset with a String
<?php $a = 'car'; // $a is a string $a[0] = 'b'; // $a is still a string echo $a; // bar ?>See the section titled for more information.String access by character