Any code when written in a perfect, straightforward and designed way is promptly acknowledged and acclaimed by everyone. It is fundamental that the codes we compose ought to have the option to be comprehended by all, in light of the fact that similar software engineers need not really take a shot at a similar arrangement of codes consistently. For simple recognizable proof and comprehension of the codes for any developer who takes a shot at it later, it is fundamental that the codes are organized, clean, made sure about and effectively maintainable.
Clarified beneath are not many of the prescribed procedures that are followed to keep up spotless and straightforward PHP codes. They are in no request for significance, so all are the practices referenced are basic and convey equivalent significance:
1.Commenting on every important action that is performed is very essential.
This not just aides in simple identification of the need of that specific code, yet additionally gives a perfect look to the codes too.
if(!$user_login){
header(“Location:https://www.macronimous.com/”);
die();
}
2.Avoid unwanted usage of conditional statements:
This expands the execution time as well as makes the coding long and complex.
For instance,
If (condition1==true){
code which satisfies the above condition
} else {
perform die(); or exit();
}
?>
The same set of codes can be written as:
if(!condition){
// display warning message.
die(“Invalid statement”);
}
?>
This decreases the execution time and furthermore makes the codes effectively viable.
The equivalent can likewise be composed as
$response_text = ( $action == “edit” ) ? “the action equals edit” : “the action does not equal edit”;
echo $response_text;
?>
Here ternary administrators have been utilized, rather than utilizing contingent explanations, to simplify the coding further.
3.Code indentation, in order to highlight statement beginnings and endings.
If(mysql_num_rows($res)>0) {
while($a=mysql_fetch_object($res)){
echo $a->first_name;
}//ending of while loop
}//ending of if condition
?>
4.Avoid unwanted html tags in the PHP code:
In the model given underneath, the PHP compiler experiences every single line of the code and executes the capacity, which is tedious.
For example:
echo “<table>”;
echo “<tr>”;
echo “<td>”;
echo “Hai welcome to php”;
echo “</td>”;
echo </tr>”;
echo “</table>”;
?>
Instead of the above we can simply say,
<body>
<table>
<tr>
<td><?php echo “Hai welcome to php”; ?></td>
</tr>
</body>
</html>
Here the PHP compiler would execute the specific worker code just, here in the model, <td><?php reverberation “Hai welcome to php”; ?></td>, rather than making html labels, as the html codes are strange to PHP. This encourages in chopping down superfluous checking season of the PHP compiler, accordingly sparing code execution time.
5.Clear Code with in assigning values to Mysql Arguments:
For example
mysql_query($sql);
In the above model, you can see that the PHP esteems are remembered for the inquiry condition. Additionally there are bunches of concatenations done to the factors inside the question.
Instead,
mysql_query(sprintf($sql,$user_id,$member_type));
By utilizing this inquiry, the qualities are consequently alloted to the fitting positions, along these lines sparing the execution time and just as the programmers can without much of a stretch locate the related qualities to the contentions passed.
6.Using Arrays:
It is in every case better to utilize clusters in PHP, as they are very simple to oversee and effectively understandable too.
$products=array(“Dove”,”Apple”,”Nokia”);
?>
Using Split array is also a very good way of coding in PHP. However, there are ways to use a split array:
Rather than:
for($iC=0;$iC<count($products);$iC++){
echo $products[$iC].”<br>”;
}
?>
Instead, the codes can be written as follows:
foreach($products as $product_value){
echo $product_value;
}
?>
foreach is explicitly for clusters. At the point when it is utilized, they lessen the coding length and furthermore the functioning time.
7.Consistent naming:
It is consistently fitting to name classes, items and others reliably. This aides in simple identification for different software engineers who may work later on the venture. Additionally names of documents in nearby indexes ought to likewise be straightforward.
8.Using Objects [class]
Despite the fact that they appear to be very convoluted to the newcomers, Objects are valuable as it lessens code repetition and furthermore encourages simpler changes to the codes. In that when a class is utilized, it makes it more adaptable to work with.
A simple class functionality is explained below:
Class shopping_cart{
var $cart_items;
function add_product_item($cart_number,$quantity){
$this->items[$cart_number]+=$quantity;
}
}
// Call the class function
$cart=new shopping_cart();
$cart->add_product_item(“123”,10);
?>
9.Appropriate use of Looping codes:
There are many circling codes accessible in PHP. It is essential to pick the correct circling code for the correct reason, with the goal that execution time can be spared upon and the work additionally would get over sooner.
For example, instead of:
for($iC=0;$iC< mysql_num_rows($res);$iC++){
echo mysql_result($res,$iC);
}
The same can be coded this way, in order to reduce the execution time:
while($obj=mysql_fetch_object($res)){
echo $obj->column_name1;
}
10.Using of case switches:
It is certainly profitable to utilize Case switches rather than If conditions. This is on the grounds that switch explanations are equivalent to utilizing a progression of IF articulations on a similar articulation.
Example of using a series of If statements:
echo “result1”;
}elseif($checking_value2==$value){
echo “result2”;
}elseif($checking_value3==$value){
echo “result3”;
}else{
echo “result 4”;
}
The same thing can be expressed in a simpler way using the switch case, which greatly reduces the operational time:
case Value1:
echo “result1”;
break;
case Value2:
echo “result2”;
break;
case Value3:
echo “result3”;
break;
default:
echo “result4”;
break;
}
11.Using single codes instead of double quotes:
In spite of the fact that these two fill different needs, utilizing single statements help in quicker execution of the circles than when utilizing twofold statements.
For instance, a straightforward case of printing 1500 lines of data, should be possible in two different ways as:
/Using twofold statements
print “SerialNo : $serialno. WorkDone : $workdone. Area: $location”;
The equivalent can be composed with single statements:
/Using single statements
print ‘SerialNo :’.$serialno.’. WorkDone : ‘.$workdone’. Location’.$location’.’;
Here, the second line of codes works a lot quicker than the first, where the strings must be analyzed totally, all the multiple times. In the second line of codes, no genuine string breaking down happens. Only blend of the strings occurs so as to make the printing conceivable.
Not many of these tips may sound very natural, however they have been incorporated here so as to revive the prescribed procedures that can be followed to get spotless, made sure about simultaneously simple to keep up PHP codes
SOURCE:-https://www.macronimous.com/resources/writing-clean-secure-and-easy-php.asp