Back to questions
JuniorTheoryCommon
What happens when you run this program?
<?php
function square(int $n): int
{
return $n * $n;
}
echo square('4'), ' ', square(2.0), "\n";
echo square('four'), "\n";16 4, then a TypeError for 'four'
- Expecting a
TypeErroralready on'4': withoutstrict_typesPHP converts a numeric string toint, sosquare('4')returns16. - Expecting
'four'to become0: a parameter typedintconverts only a string that holds a number; any other string throws aTypeErrorat the call.
- What would
square(2.5)print, and which notice comes with it? - How does
declare(strict_types=1)change the callsquare('4')?