Hướng dẫn php 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 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:

  • Numeric
  • String
  • Logical
  • Integral and string
  • Comparative
  • Function

Note: 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 Type casting. To change the type of a variable, see the settype[] function.

Numeric contexts

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.

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.

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.

Warning

Internal functions automatically coerce null to scalar types, this behaviour is DEPRECATED as of PHP 8.1.0.

Type Casting

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] - 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

Note:

[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.

Warning

The [real] cast alias has been deprecated as of PHP 8.0.0.

Warning

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.

Caution

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.

Note:

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:

Note: Instead of casting a variable to a string, it is also possible to enclose the variable in double quotes.

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

Note: 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.

Raja

17 years ago

Uneven division of an integer variable by another integer variable will result in a float by automatic conversion -- you do not have to cast the variables to floats in order to avoid integer truncation [as you would in C, for example]:

$dividend = 2;
$divisor = 3;
$quotient = $dividend/$divisor;
print $quotient; // 0.66666666666667

Anonymous

1 year ago

Cast operators have a very high precedence, for example [int]$a/$b is evaluated as [[int]$a]/$b, not as [int][$a/$b] [which would be like intdiv[$a,$b] if both $a and $b are integers].
The only exceptions [as of PHP 8.0] are the exponentiation operator ** [i.e. [int]$a**$b is evaluated as [int][$a**$b] rather than [[int]$a]**$b] and the special access/invocation operators ->, ::, [] and [] [i.e. in each of [int]$a->$b, [int]$a::$b, [int]$a[$b] and [int]$a[$b], the cast is performed last on the result of the variable expression].

fardelian

9 years ago

Casting objects to arrays is a pain. Example:



Yes, that looks like an array with two keys with the same name and it looks like the protected field was prepended with an asterisk. But that's not true:



The char codes show that the protected keys are prepended with '\0*\0' and private keys are prepended with '\0'.__CLASS__.'\0' so be careful when playing around with this.

Anonymous

3 years ago

"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 integers, and the result will also be an integer. 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."

I understand what the doc is trying to say here, but this sentence is not correct as stated, other types can be coerced into floats.

e.g.

Chủ Đề