Use Switch and Avoid Multi if-elseif statements

25 October 2011

There are few coding practices that developers can adopt to improve the performance of their PHP based projects. Better scripts execute faster and reduce server load as well. Thus, reducing page load time. So, the argument is how to write a better script and which script is better. Some will argue that PHP is an interpreted language and has its own limitations. I am listing a few of the performance tips; though some are very common and are application to any programming language while others are universally applicable to web based projects in almost any server side scripting language.

 

Use Switch and Avoid Multi if-elseif statements

 

Well, we all have been taught this in our colleges. But at times, either we ignore or the cases keep evolving during the course of development and we end up with a multiple if-elseif set of conditions. If-elseif should be adopted for 2 or a maximum of 3 conditions. Beyond that Switch statement is the way to go. It is designed to handle many conditions so it is advised to use a Switch instead of multiple if-else.

 

1 //Ignore Multiple If-ELSEIF
2  
3 if($day==1) {
4  
5 // Code Logic
6  
7 }
8  
9 else if
10  
11 ($day==2) {
12  
13 // Code Logic
14  
15 }
16  
17 else if($day==3) {
18  
19 // Code Logic
20  
21 }
22  
23 else if($day==4) {
24  
25 // Code Logic
26  
27 }
28  
29 else if($day==5) {
30  
31 // Code Logic
32  
33 }
34  
35 else if($day==6) {
36  
37 // Code Logic
38  
39 }
40  
41 else  {
42  
43 // Default Code Logic
44  
45 }
46  
47 // PREFER SWITCH
48  
49 switch ($day) {
50  
51 case 1:
52  
53 // Code Logic
54  
55 break;
56  
57 case 2:
58  
59 // Code Logic
60  
61 break;
62  
63 case 3:
64  
65 // Code Logic
66  
67 break;
68  
69 case 4:
70  
71 // Code Logic
72  
73 break;
74  
75 case 5:
76  
77 // Code Logic
78  
79 break;
80  
81 case 6:
82  
83 // Code Logic
84  
85 break;
86  
87 default:
88  
89 // Default Code Logic
90  
91 break;
92  
93 }

 

Use mysqli instead of mysql extension

 

For dynamic websites, database operations are prone to take considerable amount of execution time. Most of the PHP based sites are backed by MySQL databases. Working on MySQL 5+, it is recommended to use PHP’s mysqli extension instead of good old mysql extension. mysqli extension has been developed for PHP to get more out of MySQL.

 

Function inside Loop declaration

 

The use of functions within loop statements eats away considerable amount of time. The used function is evaluated each time the loop is incremented. And, its really not worth it when the function is returning the same value.

 

1 //BAD PRACTICE
2  
3 for($i==0;$i<count($votes);$i++) {
4  
5 // Code Logic
6  
7 }
8  
9 // GOOD PRACTICE
10 $vote_count = count($votes);
11  
12 for($i==0;$i<$vote_count;$i++) {
13  
14 // Code Logic
15  
16 }

 

Single and Double Quotes

 

Most of us use single and double quotes alternatively without realising the purpose and use of the two. Whenever we use a double quote in output statement, PHP actually takes a look at the content to find any variables in the string.While a single quote is outputted straightaway without any lookups.

 

1 // Not recommended to use double quotes in static output
2  
3 echo "There are no variable in this statement";
4 // Without variable
5 echo 'There are no variables in this statement';
6  
7 $name = "value";
8 // With a variable
9 echo 'The output includes the name variable having value '.$name.' variable';

Moreover, a comma separated concatenation will work faster than a dot seperated concatenation.

Not recommended, yet double quotes can be used with variable names as

1 echo "The output includes the name variable having value $name  variable";
2 // With a variable that is interpreted at runtime and replaced by its value.

 

Use Full or absolute Path

 

Most of us have seen and used the file paths declared with “../” or “../../” kind of paths. These declarations are said to be using relative paths. The relative paths in PHP code means that PHP has to interpret, resolve, locate and then take suitable action. The action can be including some file.

 

Unset heavy variables

 

It is always a good practice to unset heavy variables like huge arrays. Unsetting few variables from a page/method containing too many variables can release some of the memory.

1 // $cities array may contains large list of cities
2  
3 foreach ($cities as $city) {
4  
5 // Your Logic
6  
7 }
8  
9 // After the work is done remove it from memory
10  
11 unset($cities);

 

Avoid print, use Echo

 

Echo is not a standard PHP function like print. Echo is infact, a language construct and it executes about 8% faster than print.

Stick to coding standards, use caching options and dont forget, Facebook uses PHP. Its just the right kind of coding and optimization which builds great scalable sites.



Sumber : Jasa Pembuatan Website dan Sistem Informasi http://klatenweb.com
Selengkapnya : http://klatenweb.com/read/2011/10/25/688/Use-Switch-and-Avoid-Multi-if-elseif-statements.html