Post Pic

Traditional VS Bracketless if-else condition

There is no denying the fact that some developers are using traditional statements and some are using bracket less. But witch one is better to use?
Iam trying to prove it by doing a simple test. I created 3 files: mytest1.php, mytest2.php and mytest3.php. Inside these I’m running PHP’s for loop which is executed exactly 1 million times and I’m checking every number if it is odd or even with if-else statements. Difference:

  • mytest1.php: traditional: if(){do_smth;}else{do_smth;}
  • mytest2.php: curly brackets: if()do_smth;else do_smth;
  • mytest3.php: colon: if():do_smth;else: do_smth;endif;

mytest1.php

<?php
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$start = $time;

for($i = 1; $i <= 1000000; $i++){

if($i % 2){
echo $i." is odd";
}else{
echo $i." is even";
}
}
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$finish = $time;
$totaltime = ($finish - $start);
printf ("This page took %f seconds to load.", $totaltime);
?>

mytest2.php

<?php
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$start = $time;

for($i = 1; $i <= 1000000; $i++){

if($i % 2)
echo $i." is odd";

else

echo $i." is even";
}
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$finish = $time;
$totaltime = ($finish - $start);
printf ("This page took %f seconds to load.", $totaltime);
?>

mytest3.php

<?php
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$start = $time;

for($i = 1; $i <= 1000000; $i++){

if($i % 2):
echo $i." is odd";

else:
echo $i." is even";

endif;
}
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$finish = $time;
$totaltime = ($finish - $start);
printf ("This page took %f seconds to load.", $totaltime);
?>

Testing

Iam running each file 5 times and then I’ll count an average time for each one. We will see if there is any difference in load time.

I am running each file 5 times and then I’ll count an average time for each one. We will see if there is any difference in load time.

mytest1.php load times:

  • 3.062667 s
  • 2.915338 s
  • 3.162933 s
  • 3.161454 s
  • 3.521963 s

Average: 3.164871 s

mytest2.php load times:

  • 2.688994 s
  • 3.250662 s
  • 3.346098 s
  • 3.001084 s
  • 2.978943 s

Average:3.053156 s

mytest3.php load times:

  • 2.747105 s
  • 3.222176 s
  • 3.197005 s
  • 3.235344 s
  • 3.391850 s

Average:3.158696 s

Conclusion

All 3 tests were close, but test #2 (curly brackets) was a bit faster than rest 2. I must say, that this if-else statement was executed 1 million times, so there is absolutely no difference in any actual project at load speed. Another thing I haven’t mentioned above is your code’s readability.

With traditional brackets code is much easier to read for humans, so I would still recommend you to stick with basic, old-fashioned if(){}elseif(){}else{} variant. Share with opinions by giving your comments.

Related posts:

  1. Country lookup with PHP via GeoIP How to get users location i.e. country, city etc using...
  2. Memcached Server Installation and Access with PHP An exclusive tutorial about MEMCACHED Server, its simple installation method...
  3. Ajax Validation with Jquery This is a very informative tutorial for developers who want...
  4. Important Website Security Tips Website Security is a very important subject and always needs...
  5. Benefits of Output Buffering If you are not using PHP Output Buffering, you have...

Related posts brought to you by Yet Another Related Posts Plugin.

Leave Your Response

* Name, Email, Comment are Required





Lunarpages.com Web Hosting






Legal Documents – Net Lawman


Get Adobe Flash playerPlugin by wpburn.com wordpress themes