Scalar Type Declarations

Scalar Type Declarations

Scalar type declarations come in two options: coercive (default) and strict.

  • coercive (default) - coercive is default mode and need not to be specified.
  • strict - strict mode has to explicitly hinted.
<?php
// Coercive mode (default)
function sum (int ...$ints)
  {
    return array_sum($ints);
  }
  print(sum(2, 6.3, '6'));
?>

Output:

14
<?php
// Strict mode
declare(strict_types = 1);
function sum (int ...$ints)
  {
    return array_sum($ints);
  }
  print(sum(2, 6.3, '6'));
?>

Output:

Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, float given, called in [...][...] on line ...