websitewriters.co.za
Standards Compliant Web Design in South AfricaOptimising your PHP code
Posted by Raoul Snyman on March 30, 2006 on 4:14 pm | In Development, PHP |Or, how to make your PHP scripts run faster.
If you’re running a site of any decent size, you know that one of the many concerns you have is that your site is fast and smooth to navigate. Below I’d like to detail a few tips on how to make your PHP scripts run faster.
Strings:
One of the ways to make your site faster is to minimize the amount that PHP needs to parse. This is particularly relevant when it comes to strings.
The double quotes (”) are really handy for embedding variables, but if you’re not embedding variables, then rather use single quotes (’). Strings with double quotes are parse for variable names and a number of slash commands, like new line (\n), tab (\t), etc. The only thing that single quoted strings are parse for is the slashed single quote (\’).
Also, since PHP still parses single quotes, escaping out of PHP and back into HTML is still marginally faster, since PHP simply sends anything that’s not within PHP tags straight to the web server. The fact that you can escape out of PHP in the middle of a function aids us in this, since if you have a function that needs to output code, you can simply escape out of PHP, dump your HTML, and then pop back into PHP to complete the rest of the function.
You can use this method with loops and if statements too, which is another advantage. For instance, take a look at the code below:
<div class="form-item">
Pages:<br />
<?php foreach ($pages as $page): ?>
<input type="checkbox" name="pages[]" id="page<?php echo($page->Page); ?>" value="<?php echo($page->Page); ?>" />
<label for="page<?php echo($page->Page); ?>"><?php echo($page->PageName); ?></label><br />
<?php endforeach; ?>
</div>
I use the shorter form of the control structures because it looks a littler neater in the HTML, and it’s easier to read.
Includes:
Another way of speeding up your PHP scripts is to cut down on the amount of code that PHP needs to parse.
If you’re using includes, which most people do, then making sure that you only include those files that you need for that particular page will make your scripts run faster. Including all the files that you might possibly need anywhere in the system just makes PHP load everything into memory, which makes your server run slower, which makes PHP run slower. And then PHP has to parse through all the included files, which takes more time as well.
One other tip to do with includes is to use the include_once() or require_once() functions rather than include() or require(), since they check to see that the code has not already been included in the system (and therefore causing conflicts).
That’s all for now. If I find any more nice tips, I’ll write another article.
1 Comment »
RSS feed for comments on this post. TrackBack URI
Leave a comment
You must be logged in to post a comment.
Powered by WordPress with
websitewriters.co.za
theme, design by Saturn Laboratories.
Entries and
comments feeds.
Valid XHTML and
CSS. ^Top^
Howzit, php is no slug but there are proactive measures you can take in order to optimize your scripts. This can be done at various levels:
1) code level - eg. while(list($sKey,$mValue) = each($aArray)) { } instead of foreach($aArray as $sKey => $mValue) is a faster way of iterating thru large 2d arrays as foreach creates a copy of the array in mem before iteration! In php5 you can foreach by ref. Also, use language constructs such as over functions eg. isset() vs is_null()
*** isset() is faster as its a language construct
2) architecture - are u a script kiddie OR an engineer? How your app processes requests and returns responses greatly affects overall performance. See PHP Architect’s Guide to Design Patterns
3) Hare and the Tortoise - does ur server-side script gradually grind to a halt as u serve more and more requests? Think scalable.
4) …. i could ramble on
but i’ll shutup 4 now
Comment by radiohead — April 4, 2006 #