qMaster the Art of PHP Code with Expert Tips


php

PHP due to its ability to integrate superior features into web application is globally most popular languages among developer community for web app development. However, coding in PHP might me tricky task for few of us. You can follow a few great tips to help you code in PHP with ease.

Master PHP As Below

1. Avoid <? … ?>
As you might be aware of “&lt;? php … ?&gt;” which is standard code declaration block used while writing a PHP script. This might work on your server but will fail to work on other servers. It is also recommended to avoid use of shortcut tags while echoing a variable as these aren’t supported everywhere. In this manner you can make your code more portable. Atleast writing PHP script with shortcut tahgs should be avoided rather you should write it as <!–?php echo $Var ?–>.

2. Comparison Operators
Comparison operators are very much useful in PHP. Although some PHP developers could not grasp the difference between comparison operators like ’==’ and ’===’. The == operator stands for equality and checks are the value on the left and right sides equal. Whereas operator === not only measure the values on either sides equal but also ensure is the variable type same on both sides or not.

Let’s take an example of PHP code where the strpos() function is used   that may return 0 indicating the string you were searching is located at 0th index otherwise, it will be present at the first position at another string. As zero is equal to FALSE in PHP so result of strpos() don’t exactly tell if something is at the beginning of a string.

//This is an example what not to code
if
(
strpos
(
$inputString
,
‘abc’
) == false ) {
// do something
}
//This is an example of good code
if
(
strpos
(
$inputString
,
‘abc’
) === false ) {
// do something
}

3. Prefer Ternary Operators Over If/Else
Instead of using if/else statement use a ternary operator. Below is code of PHP that better illustrate ternary operator.

1
$todo
= (
empty
(
$_POST
[’todo’])) ? ‘
default
’ :
$_POST
[’todo’];
This line of code is equivalent to the following if/else statement:
123456
if
(
empty
(
$_POST
[’todo’])) {
$action
= ‘
default
’;
}
else
{
$action
=
$_POST
[’todo’];
}
?&gt;

As it is evident in the above code how smoothly has ternary operator made the code clutter-free thereby, saved a plenty of space.

4. Stress on use of Relevant Variable Names
A very important step to follow while writing PHP code is to choose variable names that holds some value as well as include details regarding data that will be stored in these variables. This is simple if you happen to use an integer try using an “i” at beginning. Coding in PHP don’t require specifying a type while declaring variable in PHP that makes it an important aspect to be concerned. More ever, try to capitalize the first letter of your variable.

5.Put Comments Along Each Important Action
Adding comments side-by-side for all important action will easily let you identify the requirement for any specific PHP code snippet especially while dealing with a very large size project. comments also ensure code look neat and tidy hence can be easily understood by your fellow developers

6. Replace ereg_replace() and preg_replace() with str_replace()
In order to boost the efficiency of PHP code to replace strings start using prefer str_replace() in place of ereg_replace() and preg_replace(). str_replace() is almost 60% more efficient in comparison to other PHP expressions

7. Naming Convention Consistency
Opting consistent naming conventions is a good coding practice. When you regularly wrie classes and objects it will be easy for your team mates to work on the project with zero confusion. Apart from this make sure files saved in local directories have a name that is easily understandable.

8. Prefer isset() Over strlen()
While finding out the length of the string choose isset() than strlen(). The isset() function will make calling 5 times faster. Also your function call will remain valid irrespective of does the variable exists or not.

About Singsys Pte. Ltd. Singsys is a solution provider that offer user friendly solution on cutting edge technologies to engage customers and boost your brand online results from a set of certified developers, designers who prefer optimized utilization of the available resources to align client’s idea with their skillset to reflect it into a Mobile application, Web application or an E-commerce solution

Tags: