This is a discussion on Website Performance Tips & Tricks within the Web Design Help forums, part of the Web Development category; Optimize Your PHP Code * Introduce your PHP-code with <?php because a simple <? may cause interference with XML-...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Optimize Your PHP Code Thanks -Kathir |
| Sponsored Links |
| |||
| Single Quotes and Double Quotes are Very Different It never recommend using " (double quotes) when programming with PHP. Always use ' (single quotes) unless you need the features of " (double quotes). You might think it's much easier to write code as: echo "Today is the $day of $month"; However, using single quotes forces variables to be outside the quotes; instead, you must use the period (.) to combine strings. It makes for faster coding but can be more difficult for other programmers to read. Let’s look at what would happen if we put an associative array value in the previous code: echo "Today is the $date[‘day’] of $date[‘month’]"; we would receive a parse error and it would be harder for another team member to read. Two correct ways to write that line of code would be: echo 'Today is the ' . $date[‘day’] . ' of ' . $date['month']; and echo "Today is the {$date['day']} of {$date['month']}"; These might not look as pretty as the original code, but syntactically they are both correct. Additionally, It is believed that the first method, with single quotes, is easier to read. The use of single and double quotes also applies to associative arrays. Consider this code: $SESSION[team] = $SESSION["old_team"]; One main problem exists in that line of code. The associative entry team on the left side needs to have single quotes around it; otherwise, PHP will think it’s a define and give you a warning message (only if error reporting is at maximum). It is recommend that the code should look like this: $SESSION['team'] = $SESSION['old_team']; From the above we had known the difference between single and double quotes as they pertain to strings.
__________________ J.Vijayanand |
| |||
| Reduce Your PHP Code Thanks -Kathir |
| |||
| Optimize Your MySQL Queries Thanks -Kathir |
| |||
| Web Catching:- It's a Technique to reduce load on backend servers by the way of serving up all static data requests(static HTML, Javascript and Image files).Thanks R.Rajan |
| |||
| For classes with deep hierarchies, functions defined in derived classes (child classes) are invoked faster than those defined in base class (parent class). Consider replicating the most frequently used code in the base class in the derived classes too. Thanks, Ramkumar.B Last edited by ramkumaraol : 08-20-2007 at 10:05 PM. |
| |||
| Calling object methods (functions defined in a class) are about twice as slow as a normal function calls. Inside a method (the following ratios are approximate only): 1. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function. 2. Incrementing a global variable is 2 times slow than a local var. 3. Incrementing a object property (eg. $this->prop++) is 3 times slower than a local variable. 4. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one. 5. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists. 6. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance. 7. Methods in derived classes run faster than ones defined in the base class. 8. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations. Update: 11 July 2004: The above test was on PHP 4.0.4, about 3 years ago. I tested this again in PHP4.3.3 and calling a function now takes about 20 $localvar++ operations, and calling a method takes about 30 $localvar++ operations. This could be because $localvar++ runs faster now, or functions are slower. Thanks, Ramkumar.B |
| |||
| Move Script Tags to Bottom Instead of Head Section: This is one of the technique to improve the website performance.Note:- Not all JS can be moved to the bottom of page. The script needs to be written as proper progressive enhancement for this technique to work.
__________________ Regards, VELHARI I am not totally useless. I can be used for a bad example |
| |||
| Query optimization Minimizing the result set in a DB Query will decrease the maximum execution time. For example to select only id & name of a member, rather than using the code like select * from member where member_id='some number'; we can use the query like the following select id,name from member where member_id='some number'; This will surely increase the performance of the page.
__________________ J.Vijayanand |
| |||
| Safe queries A safe query will not return an error message that may reveal path information or give hackers accidental insider information. Certainly, security by obscurity is not an effective measure, but reducing error messages at the user end is desired once your site is launched. Our first function performs the actual query using msyql_query. If the query string is empty it will return false. PHP Code: PHP Code: PHP Code: PHP Code:
__________________ J.Vijayanand |
| |||
| Duplicate Scripts: This is also one of the reason for to reduce the performance of the website and of course you might think its an unusual thing.
__________________ Regards, VELHARI I am not totally useless. I can be used for a bad example |
| |||
| Template systems Template systems provide a different type of caching. Content caching. Template systems work well in a situation where there is static data on one or many of your pages that doesn’t have to be reloaded. Caching systems also provide a separation of code and html, which will not only improve completion time of the overall project, but make it easier for future improvments. Most template systems for php are available for free(like smarty ect..).
__________________ J.Vijayanand |
| |||
| PHP variables that can be set variables_order = ‘GPC’ register_argc_argv = ‘Off’ register_globals = ‘Off’ (this is a good idea to keep off for security purposes as well) always_populate_raw_post_data = ‘Off’ magic_quotes_gpc = ‘Off’ Disable Error Logging. This is a good idea to keep on when you are developing your scripts, but it has been known to decrease overall performance. Use IP address to access your database. Although this is sometimes not possible, you will get a slight boost in lookup speed if the IP address is used to access your database rather than its hostname.
__________________ J.Vijayanand |
| |||
| Use output buffering Thanks -Kathir |
| |||
| Tips for increase the site performance
__________________ J.Vijayanand |
| |||
| Compress your data Thanks -Kathir |