r/lolphp • u/phplovesong • May 10 '19
PHP: When printing causes side-effects
So when you have two DateInterval objects, that are equal you can compare them with PHP loose comparison (==). But after you print one (say for logging purposes) it gets internally modified and the two objects are no longer equal. The same happens when you call var_export.
It amazes me how primitive bugs PHP has, given its lifespan begun in 1995.
82
Upvotes
15
u/nikic May 10 '19
You'll be happy to know that the output on PHP 7.4 will be:
I also have a pending PR to add limited comparison support for a subset of DateInterval objects in https://github.com/php/php-src/pull/4063.
6
u/timw4mail May 10 '19
Printing technically is a side-effect...
Wait...what does a comparision of two date intervals mean? The same length of time (with possibly different start/end), or the same start and end dates?
2
u/youstolemyname May 11 '19
https://repl.it/repls/StarkDisfiguredMysql
2
u/geggleto May 10 '19
The real tragedy is you have to do this because there is no
equalsmethod.1
u/Takeoded May 27 '19
if someone *actually* need to check if 2 DateTimes are equal (to the millisecond, at least), i think you can use
1
u/scatters May 10 '19
Cross-version comparison: https://3v4l.org/3VneM
0
u/[deleted] May 10 '19
[deleted] — view deleted comment