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

if ( $itzurkarthi == true ){
    $value = 'yes';
}else{
    $value = 'no';
}

Now using only IF

$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.

$value = 'no';
if ( $something == true )
    $value = 'yes';

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

$value = ( $something == true ) ? "yes" : "no" ;

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

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

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:

$html = "<input name='" . $name . "' id='" . $id . "' value='" . $value . "' class='" . $theclass . "' type='checkbox' " . $checked . " />";
echo $html;

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

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

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

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

The more server friendly way to do things

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

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.

Thanks!

Source – link

If you enjoyed this article, Get email updates (It’s Free)
http://w3lessons.info/wp-content/uploads/2011/12/IMG_29591-1024x711.jpghttp://w3lessons.info/wp-content/uploads/2011/12/IMG_29591-150x150.jpgKarthikeyan KPHPtips & tricks,website speed
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...