Cast Operator

Cast operators change the type of the variable to the desired type. Conversion between the current format and the target format may happen.

The available casts are:

  • (int), (integer): cast to integer

  • (bool), (boolean): cast to boolean

  • (float), (double): cast to float

  • (string): cast to string

  • (array): cast to array

  • (object): cast to object, of type stdclass. There is no operator to convert to a specific class type.

  • (void): complement operator to the NoDiscard attribute. It is not really a cast operator.

  • (unset): cast to NULL, deprecated since PHP 7.2

  • (real): cast to float.

<?php

    $foo = 10;               // $foo is an integer
    $bar = (boolean) $foo;   // $bar is a boolean

?>

Documentation

See also A detailed look into PHP type casting, Mastering Data Type Conversion In Php Through Type Casting, Conversion and Mastering the (array) cast.

Related : __toString() Method, Type Juggling, ArrayObject, Convert, Non-numeric, Sign, array_keys(), Boolean, Golf, Number, Canonical, get_object_vars()