Every once in a while it helps to visit your coding habits and see if there are areas that can be improved upon. For some programmers, this could mean a complete overhaul of their coding methods while for others it might just mean a few adjustments here and there. This article explains 3 easy to implement tips and tricks which can make your PHP code quite a bit more efficient.
1. To Quote or to Double Quote
Most programmers, especially beginners, use single quotes and double quotes interchangeably most of the time. For example, the following two PHP statements might be considered identical by some PHP programmers:
echo ‘John Doe’; echo “John Doe”;
Both these statements might be identical as far as the outputs (which would be John Doe in both cases) are concerned.
There is, however, a significant difference in the way these two statements are processed. PHP processes and evaluates the contents enclosed by double quotes while it simple considers contents enclosed in single quotes as strings.
Let’s look at an example to understand what this means.
|
With Single Quotes |
Double Quotes |
Code | $var1 = ‘apple’;
$var2 = ‘$var1’; echo $var2; |
$var1 = ‘apple’;
$var2 = “$var1”; echo $var2; |
Output | $var1 | apple |
As you can see in the above table, PHP evaluates $var1 when it is enclosed in double quotes to assign the value apple to $var2. However, when enclosed in single quotes, PHP assigns the value $var1 literally to $var2.
This means that unless you have a variable in a string, you are much better off using single quotes as it results in less overheads and faster execution.
Verdict: Use single quotes unless you necessarily need to use double quotes.
2. Increment/Decrement Variables Efficiently
One of the most common programming statements includes incrementing/decrementing a variable by one. PHP provides two operators (++ and –) which do exactly this but can be used in a couple of different ways.
Pre |
Post |
|
Usage | ++$i OR –$i | $i++ or $i– |
Example | $i = 5;
$var1 = ++$i; echo $var1; |
$i = 5;
$var1 = $i++; echo $var1; |
Output | 6 | 5 |
As you can see in the table above, pre-incrementing (or pre-decrementing) performs the operation first and then assigns the value to the variable. On the other hand, post-incrementing (or post-decrementing) first assigns the existing value and then performs the increment/decrement operation on the variable. In both cases, the value of $i increases (or decreases) by one but $var1 gets assigned a different value.
In post incrementing, PHP first checks whether the value needs to be assigned, then performs the assignment and finally performs the increment operation on the variable. On the other hand, there are no checks while pre incrementing. PHP simply increments the variable and then treats the result as any other variable.
Verdict: If you need a standalone increment/decrement statement, it is more efficient to use pre-increment (or pre-decrement) compared to post-increment (or post-decrement).
3. Smart Hack To Check For Minimum String Lengths
Most PHP scripts which validate user inputs often need to ascertain that a string has a minimum length before it is accepted. The common method of doing that is by using the function strlen($strvar) which returns the length of the string variable passed as a parameter to it.
This function requires PHP to actually check the length of the string which takes more time compared to another quick method which uses the isset($var) function of PHP.
We know that the function isset() is used to check if the variable passed to it as a parameter has been set or not. We also know that a string in PHP is nothing by an array of characters in which each character can be accessed by its index. By combining these two bits of information, we can check for a minimum length using isset() quite efficiently.
Using strlen() |
Using isset() |
|
Code | $var1 = “string”;
$minLength = 6; echo (strlen($var1)>=$minLength); $minLength = 7; echo (strlen($var1)>=$minLength); |
$var1 = “string”;
$minLength = 6; echo (isset($var1{$minLength-1})); $minLength = 7; echo (isset($var1{$minLength-1})); |
Output | 1 (indicating true)
Blank (indicating false) |
1 (indicating true)
Blank (indicating false) |
As you can see from the above table, the responses of the two methods are identical. However, using the isset() method requires considerably less overhead and is executed much quicker compared to the strlen() method which requires calculating the length and then comparing it.
Verdict: Use the isset() function instead of the strlen() function in conditional checks involving the lengths of strings.
The benefits of these three programming practices might not be obvious in a small script. However, when these issues are repeated over and over again in a larger application, every little optimization can make your code quicker. Not only will this reduce the burden on your server, it will also result in a much better user experience.
Source: New feed