Writing clean code with security and easy to maintain in PHP
This quote says it all for the coders. Code written should be easy to understand, clean and formatted. It’s the cycle where one developer will leave the code for next developer to work on it. So we need to keep the code as clean, structured and maintainable so that If the guy who works after you knows you.
Let's explore some code habits that we use in daily code stuff and behave live clean and maintainable structure.
Importance of these practices which have equal importance.
These are not in any order of maintenance, So we can follow any of them as we require.
- Comments
Helps in easy identification of the need of that particular code, but also gives a neat look to the codes as well.
- Conditional Statements
It makes things look ugly and performance also degrades. Become more complex and code goes long.
Before:
<?php
if (condition==true){
//we are wotking fine
} else {
//Halt operation die(); or exit();
}
?>
After:
<?php
if(!condition){
// Halt operation.
die();
}
?>
- Code Indentation
<?php
if($res>0) {
while($res){
echo $res->first_name;
}//ending of while loop
}//ending of if condition
?>
- Unwanted HTML Tags
Compiler goes through each and every line of the code and executes them, which is time consuming.
Before:
<?php
echo "<table>";
echo “<tr>”;
echo “<td>”;
echo “Learn the right way”;
echo “</td>”;
echo </tr>”;
echo “</table>”;
?>
After:
<html>
<body>
<table>
<tr>
<td>
<?php echo "Learn the right way"; ?>
</td>
</tr>
</table>
</body>
</html>
Compiler would execute the particular server code only <?php echo "Learn the right way"; ?>
This facilitates in cutting down unnecessary checking time of the PHP compiler, thereby saving code execution time.
- Mysql Arguments Assignment
Do not use the code to push direct values to the mysql queries as we can write clean code in a way so that automatic assigned to the appropriate positions.
It will save execution time and easy to use/maintain.
Before:
$sql="select first_name,last_name,email_address from tbl_user where user_id=".$user_id." and member_type='".$member_type."'";
mysql_query($sql);
After:
$sql="select first_name,last_name,email_address from tbl_user where user_id="%d" and member_type='"%s"'";
mysql_query(sprintf($sql,$user_id,$member_type));
- Using Arrays
Better to use the arrays as foreach saves functional timing and coding length also.
Try to use various arrays as per the requirement and save both coding length and functional timings.
- Consistent Naming
Always use the name consistent to the work which is supposed to do by the class/object or variable/function.
- Using Objects/Class
It makes code maintainable, reduces code repetition, makes things go on a flexible way.
Might be learning curve for newcomers but effective in long run.
- Use of Loop
Choosing the right loop method is high priority.
It will lead to the overall process time. There are various ways we can use loop in PHP but we need to analyse the fastest.
- Use Of Switch
While working with conditional things usually developers go on for if else cases which can be easily tracked down by using switch case.
It greatly reduces the operational time.
- Use Single Quotes instead of Double Quotes
Single Quote is faster in execution than double.
It helps to loop executes faster which can be analyzed by running this.
Same thing can be achieved by both quotes while working.
So happy coding :)