Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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