Tips to Speed Up your PHP Website/Application

Last modified on by Karthikeyan K

Your website acts as a communication interface between your office/organization and your customers/visitors/users.  Slow loading websites causes users to close the webpage. Slow speed websites are used in google ranking algorithms . So here I am going to tell how can you make your website faster using simple php tips and tricks

Making IF ELSE smaller

IF is possibly the most used statement from all the programming languages. Lets see here how we can improve the If else statement

Common IF ELSE usage

[php]if ( $itzurkarthi == true ){
$value = ‘yes’;
$value = ‘no’;

Now using only IF

[php]$value = ‘no’;
if ( $itzurkarthi == true ){
$value = ‘yes’;

As you can see all that has been done is that value is pre-set to ‘no’ and only gets changed to ‘yes’ if something is true. Both bits of code have the same outcome, one has less code.

If you only have 1 command inside the if statement then you can take things a little further. PHP does not require that you have the curly brackets when there is just one line.

[php]$value = ‘no’;
if ( $something == true )
$value = ‘yes’;[/php]

Ternary operators

We can go a stage further and have Ternary Operators. These are mathematical instructions that allow for a yes/no answer, just like IF.

IF presented as ternary operation

[php]$value = ( $something == true ) ? "yes" : "no" ;[/php]

Again the same IF ELSE statement, made much smaller. What it does is check the if the statement inside the brackets is either true or false, if it is true it will set value to be “yes”, if it is false it will set value to be “no”

syntax of ternary operators

[php]output = ( true/false condition ) ? true-value : false-value ;[/php]

It is not essential to have an output, as the true or false value could call a function and not return a value.

Making your code easier to maintain, and faster

I often see the same problem with my noob code when I re-visit it. The code is all written on 1 line and very long. Check this bit of code as an example:

[php]$html = "<input name=’" . $name . "’ id=’" . $id . "’ value=’" . $value . "’ class=’" . $theclass . "’ type=’checkbox’ " . $checked . " />";
echo $html;[/php]

It works just fine, and will be ok for any implementation, but there are some problems with it. For example if the class is empty, it is wasteful to code the empty class setting. While this may not make so much difference to a Javascript implementation, as this is being generated on the server with PHP, you’d be sending extra bytes to the clients browser which are not required. This of course will make the most difference when making AJAX calls and waiting for the reply, where extra bytes can make much more of an impact.

An easier way to maintain

[php]$attributes = " name=’" . $name . "’ ";
$attributes .= " id=’" . $id . "’ ";
$attributes .= " value=’" . $value . "’ ";
$attributes .= ($theclass != ” )? " class=’" . $theclass . "’ " : "" ;
$attributes .= ($selected == $value )? " checked " : "" ;
$html = "<input type=’checkbox’ " . $attributes . " />";
echo $html;[/php]

As you can see, much easier to read, much easier to maintain, and used on the server side will produce less HTML code.

Don’t echo every line only the final output

It’s much easier to echo every line, or pass control back to HTML, than it is to write every line to a variable, yet writing the output to a variable first and only echoing at the end is the faster way to process

The easy way to do things, with a few variations

[php]echo "<h2>" . $title . "</h2>"; ?>
<small><?php echo date(); ?><small>[/php]

The more server friendly way to do things

[php]$output = "<h2>" . $title . "</h2>";
$output .= "<small>" . date() . "<small>";
echo $output;[/php]

OK, this is a very simplistic example of what you should do as it is only 2 lines and then output.

Please don’t forget to share and subscribe to latest updates of the blog.


Source – link

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *