Normally I write code by using the conventional, obvious PHP functions to solve corresponding problems. But for some of these problems I came acros...
For further actions, you may consider blocking this person and/or reporting abuse
I readed somewhere that double quotes are more than double fast, it really surprised me.
I can confirm that the single vs. double quotes myth is really just a myth. At least for the PHP 7.2+ (maybe this originates from very early PHP versions). I did a quick benchmark:
You can say with caution, that in average, double quotes are slightly (and I really mean very slightly!) faster than single quotes. We're talking about a max of 2ms on 5 million iterations... Obviously only, if no variable is replaced.
Double quotes appear to be slower than single quotes because PHP parses their content for variables, e.g.:
"a house with $n windows"
, but on the other hand, single quotes are parsed as well, e.g.'a \'quoted\' string'
I'm looking for the source, that's why it surprised me, it has no sense.
There it is, simple quote are faster only when it creates an empty string xD
phpbench.com/
It looks like there is really no significant difference. See this tweet.
Thank you for this link, this was tested with PHP 7.2, I will retest with the current version of PHP and will present the result 👍🏻
Well, it's not that surprising, considering double quotes have to process the content.
Re read, you will see the surprise!
For what its worth, ctype_ functions are locale aware, and may produce slightly different results based on the locale. While preg_match is not locale aware.
Thank you for this addition! I couldn't find something in the docs about the locales, do you have an example?
It is mentioned in the introduction on ctype php.net/manual/en/intro.ctype.php
Ah thanks 😊
And about the simple or double quotes? What your opinion about the performance of both?
Do you mean quotes? If you're interested, I could add a
'
vs"
test...Yes, quotes, my bad. Did you make some implementation of it?
Not yet, but I will create a test with the current PHP version and will come back when I have results 👍🏻
In the meantime, see this:
I did a quick benchmark, see this comment:
I can confirm that the single vs. double quotes myth is really just a myth. At least for the PHP 7.2+ (maybe this originates from ver early PHP versions). I did a quick benchmark:
You can say with caution, that in average, double quotes are slightly (and I really mean very slightly!) faster than single quotes. We're talking about a max of 2ms on 5 million iterations... Obviously only, if no variable is replaced.
Great! Just for the convention is used so. Thanks, Andreas.
My pleasure 😊
Nice tricks! I personally never tried the 2nd approach for getting random items from an array, although it's quite obvious haha. Thanks.
😂 well, everything's fine than I guess! How'd you do it?
Helpful article, thank you very much =)
You're welcome, I'm glad it's useful for you 😊