Earlier quoted context omitted.
Actually this type of over-usage of ternary operators is not a PHP thing.. it's very prevalent in the JavaScript world
None of those snippets contain a ternary operator. ?? is the Null Coalescing Operator[1] ?-> is the nullsafe operator[2] [1]: https://www.php.net/manual/en/language.operators.comparison.... [2]: https://www.php.net/manual/en/language.oop5.basic.php#langua...
Algorithms in PHP: Deques
21–25 of 25 posts
Re: Algorithms in PHP: Deques
#22Php's array_shift() does seem unusually slow. $ time php -r '$a=array_fill(0,100000,"test");while(array_shift($a)){}' real 0m10.238s user 0m10.238s sys 0m0.000s $ time perl -e 'my @a=("test") x 100000;while(shift(@a)){}' real 0m0.027s user 0m0.027s sys 0m0.000s $ time node -e 'a=Array(100000).fill("test");while(a.shift()){}' real 0m1.350s user 0m1.343s sys 0m0.011s
PHP arrays are associative arrays, it has to update the keys for every remaining value after the first element is deleted.
Re: Algorithms in PHP: Deques
#23Php's array_shift() does seem unusually slow. $ time php -r '$a=array_fill(0,100000,"test");while(array_shift($a)){}' real 0m10.238s user 0m10.238s sys 0m0.000s $ time perl -e 'my @a=("test") x 100000;while(shift(@a)){}' real 0m0.027s user 0m0.027s sys 0m0.000s $ time node -e 'a=Array(100000).fill("test");while(a.shift()){}' real 0m1.350s user 0m1.343s sys 0m0.011s
PHP arrays are associative arrays, it has to update the keys for every remaining value after the first element is deleted.
But a tradeoff in that design is it still has to keep track of keys and update them if you remove stuff from the start of the array. Adding and removing elements at the end is very fast, though.
Re: Algorithms in PHP: Deques
#24Earlier quoted context omitted.
None of those snippets contain a ternary operator. ?? is the Null Coalescing Operator[1] ?-> is the nullsafe operator[2] [1]: https://www.php.net/manual/en/language.operators.comparison.... [2]: https://www.php.net/manual/en/language.oop5.basic.php#langua...
Oh my mistake. Still stuff like 'return a || b' is the kind of thing I see in JS way more than in PHP codebases
It was an okay-ish way to get a default/fallback value before JS got a proper coalescing operator.