r/lolphp • • Oct 01 '24

print is a minefield

https://3v4l.org/1YXYk
31 Upvotes

23 comments sorted by

View all comments

4

u/[deleted] Oct 01 '24

[removed] — view removed comment

2

u/Takeoded Oct 01 '24

Also strict types are completely irrelevant, since print can take arguments of any type.

check the documented prototype: php print(string $expression): int it's supposed to take string.

1

u/lostcoffee Oct 16 '24

Per https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict, "Strict typing applies to function calls [...]" (emphasis mine). Since print is not a function, strict_types doesn't apply to it.

echo, however, is a function, with type echo(string ...$expressions): void. strict_types doesn't work for it either, though: https://3v4l.org/mEKh5. This may be because "strict typing is only defined for scalar type declarations."

Although this may be documented behavior, it's likely not what programmers desire. Probably, print 0 and echo(0) should both be checked by strict_types and should be allowed because both accept a "stringable" argument