Type JugglingPHP 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. PHP may attempt to convert the type of a value to another automatically in certain contexts. The different contexts which exist are:
To force a variable to be evaluated as a certain type, see the section on Type casting. To change the type of a variable, see the settype function. Numeric contextsThis 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 contextsThis is the context when using echo, print, string interpolation, or the string concatenation operator.
In this context the value will be interpreted as string.
If the value cannot be interpreted a TypeError is thrown.
Prior to PHP 7.4.0, an Logical contextsThis 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 contextsThis is the context when using 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 contextsThis 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 contextsThis 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 int and the declared type is float, then the integer is converted to a floating point number. The second one is: if the declared type is a scalar 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. Warning
Internal functions
automatically coerce Coercive typing with simple type declarations
Coercive typing with union types
When
Caution
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
numeric string
semantics.
For example, for
Example #1 Example of types being coerced into a type part of the union
Type CastingType casting converts the value to a chosen type by writing the type within parentheses before the value to convert.
The casts allowed are:
Warning
The Warning
The Caution
The
Casting literal strings and variables to binary strings:
It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections:
|