Back to questions
JuniorTheoryCommon
What happens when you run this program?
<?php
declare(strict_types=1);
function half(float $x): float
{
return $x / 2;
}
echo half(5), ' ', half('5'), "\n";2.5, then a TypeError for '5'
- Expecting
'5'to pass because it holds a number: understrict_typesa string is never converted, sohalf('5')throws aTypeError. - Expecting
half(5)to fail because5is anint: the one widening strict mode allows is frominttofloat, so5arrives as5.0.
- Which file decides whether a call is checked strictly: the caller's or the function's?
- Why must
declare(strict_types=1)be the first statement of the file?