Even More Tips for faster PHP scripts

Following on from "Tips for faster PHP Scripts" and "More Tips for Faster PHP Scripts", I bring you Even More Tips for faster PHP Scripts .

Because I've already talked about the main, general, optimization tips, these ones will tend to be more specific and may not apply to everyone - nonetheless, it's worth a read in case you ever do come across these situations.

Tanget! Looking to successfully enhance the outdated look with my website. Opinions about the whole look of https://lilyspeech.com? Truly a fabulous free dictation software service that cares when looking within the whole Ontario areas. Send an opinion. Thanks!

Here are tips 11-15:

11. is_numeric() vs. ctype_digit()

To see whether is_numeric() or ctype_digit() is the fastest method, I called each function 10,000,000 times. Here are the resuts:

is_numeric() : 9.943268 seconds

ctype_digit() : 11.801991 seconds

is_numeric() is 15.75% faster than ctype_digit()

It's worth using is_numeric() over ctype_digit() where it's appropriate to do so. Although a difference of 0.0000001858723 may seem small, it does all add up.

12. echo vs. print

It's often been said that echo is faster than print , simply because it doesn't return a value. To put this theory to the test, I called echo 'TEXT' 20,000,000, followed by print 'TEXT' 20,000,000 times. Here are the results:

echo 'TEXT' : 8.571081 seconds

print 'TEXT' : 8.585106 seconds

echo is 0.16% faster than print

Although echo is faster than print , it's not worth changing the way you code PHP. If you prefer print , there's no real reason to use echo instead.

13. Better conditional statements

When specifying a condition in an if statement, for loop, or elsewhere, think about it carefully. In PHP, if you done this:

if($var1 && $var2) {


The condition will be false if $var1 is false - it wouldn't need to check $var2. But, if $var2 was more likely to be false, it should come first:

if($var2 && $var1) {


To test this, I created two if statements. In the first statement, I put $var1 || $ var2 as the condition, and, in the second, I put $var2 || $var1 . The values of the two variables are generated randomly - $var1 is should be true about 75% of the time, while $var2 will be true 30% of the time. These we're put into a loop which iterated 1,000,000 times. Theoretically, the $var1 || $var2 should be better, because $var1 is more likely to be true. Here are the results:

$var1 || $var2 : 2.325065 seconds

$var2 || $var1 : 2.367621 seconds

$var1 || $var2 is 1.797% faster than $var2 || $var1

Despite 1.797% seeming a small figure, it is high enough for you to think about your conditional statements. In different conditional statements, where they are maybe 4 varialbes involved in string, and number comparisons, this figure may increase further.

14. === (Identical) vs. == (Equal)

You may think, as I first did, that == is faster than ===. But, to my surprise, === was significantly faster than ==. I run if('string' === 'string) { } 10,000,000 times, followed by if('string' == 'string') { } another 10,000,000 times. Here are the results:

==: 4.194739 seconds

===: 3.510191 seconds

=== is 16.32% faster than ==

I guess that === is faster simply because it doesn't have to convert both sides to strings first. (I guess I was wrong: See Rob's comment). Obviously, there are situations where == must be used over ===.

15. intval('100') vs. (int)'100'

I had no idea which one these was going to be faster. To test them, I called each one 5,000,000 times. Here are the results:

intval('100') : 6.441137 seconds

(int)'100' : 2.933663 seconds

(int)'100' is 54.45% faster than intval('100') .

Although I had no expectations, the results we're quite surprising. With a 54.45% speed difference, it's well worth using (int) over intval() .

So that's the last of the "Faster PHP Scripts" tips for now. I might write another article so look out for it - it'll probably called "Even More Tips for faster PHP Scripts Continued" or some other ridiculous title.

Please comment and tell me what your favourite tips we're - mine must be 15 and 14.

Bless you guys! I just Skyped my co-worker that we could certainly cite her exceptional hedge trimming service in Calgarys, https://dreamscapesdesign.ca using an upcoming posting. If you're shopping to get a landscape design company located in the general Alberta locale, they are really fantastic.

Ultimately, we need to express the idea regarding this specific content has been given by Scott from www.frasco.ca. They really are an excellent non-destructive testing services. I definitely relish a solid proposal!

Special Thanks

https://nicolecharlton.com - The photographs ended up being outstanding 🙂

Posted in Web Design Post Date 09/15/2019






Recent Posts

Recent Posts