Flexible programming language for me is PHP regarding my programming experience. There is more than one way to achieve the same result when programming in PHP. It depends on the programmer’s problem analysis, way of coding and the dependent conditions itself.
These are all same :
$variable = $variable + 1;
$variable ++;
$variable = $variable - 1;
$variable –;
Concatenate string :
$mystring = ‘I am good,’;
$mystring .= ‘ coz i do good.’; // fast programmer style
$mystring = $mystring . ‘ coz i do good.’; // lazy way
Single and Double Quotes :
Not having proper knowledge of using single and double quotes often eats up processing time. Any time you put something in “double” quotes, you are asking PHP to check that content for a variable. Where as, if you use a variable inside single quotes will result the variable as a string itself.
$myvariable1 = “I can use this way.”;
$myvariable2 = “This phrase has a variable. $myvariable1″; // proper use of double quotes
$myvariable3 = ‘This phrase does not have a variable. No need to let php check inside this block for a variable.’;
For better performance use shorthands for condition checks :
if($mycondition == “matched”)
echo ‘This condition doesn\’t have a brases {} as this is a single line statement inside the condition block.’;
else{
$myflag = false;
echo ‘Else condition has more to do. So it has brases {}’;
}
if($mytrueCondition || !$myfalseCondition)
echo ’statement goes here’; // no need to write if($mytrueCondition == true)
function names are always lowercase
function myFunction(){} // is same with myfunction(){}
The fewer brackets you have cluttering up your code, the easier it may be to read.
foreach ($kill as $count)
echo ‘Legolas strikes again, that makes’ . $count . ‘for me!’;
Mix up conditions with HTML :
<?php if($mycondition){ ?>
<div> This is a true result block</div>
<?php }else{ ?>
<div> This is a false result block</div>
<?php } ?>
Or, use a more secure way to companion with HTML :
$myoutput = ‘<div style=”mystyle”> This is good way of implementing HTML with php</div>’;
echo $myoutput;
Swap form post values in respective form element named variables for ease :
foreach($_POST as $key => $value){
$$key = $value; // swaps form post values creating dynamic variables of form elements
}
Better coding with performance tuning and easy debugging :
$mytxt = “This is my last phrase.”;
echo “This is my first phrase.”;
echo “This is my second phrase.”;
echo “This is my third phrase.”;
echo $mytxt;
$myPhrase = ‘This is my first phrase.’
. ‘This is my second phrase.’
. ‘This is my third phrase.’
. $mytxt;
echo $myPhrase;
Normal if else syntax :
if( $color == ‘green’ ) {
echo ‘Green’;
} else {
echo ‘Not Green’;
}
The statement will do the exact but with more readably :
echo ($color == ‘pink’ ? ‘Pink’ : ‘Not Pink’);
Query database :
$sql = “SELECT
tbl1.Field1
,tbl1.Field2
,tbl2.Field1
FROM
tbl_Table1 as tbl1
tbl_Table2 as tbl2
WHERE 1 = 1
AND tbl1.ID = tbl2.ID”;
if(!empty($myid))
$sql .= ” AND tbl1.ID =$myid”;
if(!empty($myName))
$sql .= ” AND tbl2.myName = ‘$myName’”;
Key : If you have any comments or better ideas or atleast want to know more plz do share ur views with me in this blog.
Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.