5+ Advanced PHP Tips To Improve Your Programming
PHP is a very versatile programming language that can achieve the same
objective in multiple ways. Below are a few tips I’ve picked up from past projects that can
improve your code readability and maintainability and make you a neater,
more organized PHP programmer.
1. Be Familiar with SQL Injection
SQL injection is dangerous and enough to allow hackers enter
your database breakhing the security by finding vulnerabilities in your
script. Most programs these are built using
PHP and MySQL. So if we want to secure our site from hacker we need to must familiar with SQL injection.
2. Know the Basics of Comparison Operators
Comparison operators are crucial part of PHP. Most programmers, while
they’re still in the learning phase, might not know the difference
between some important comparison operators as they need to.
Each operator has their own significance while using them to compare
between two values. For example, many PHPers don’t know the difference
between =, ==, and === operators. While ‘=’ assigns a value, ‘==’ looks
for equality, and then ‘===’ looks for identity.
3. Conserve the else Statement
Making code simpler and smaller is a good practice.
Here’s an example.
A typical else statement looks like this:
if(condition is true){
$a=3;
}
else{
$x=5;
}
If the $a will have the default value of 5, then just start with it.
Don’t bother using the ‘else’ statement.
$a=5;
if(condition is true){
$a=3;
}
This might not seem a lot, but if you’re coding a hundred lines of
code in each page, then this trick will surely save you a lot of time
involved while writing various if/else statements..
4. Drop Brackets
Drop unnecessary brackets while coding to save few characters in your
PHP code. Just like using the minimal else statement, you can minimize
the file size by dropping brackets for single-line expression.
if($result){
print “I am here.”;
}
Is as same as:
if($result) $print “I am here.”;
Do the same for multiple stances.
if($result==’true’) print “I am here.”;
else print “Okay, Print this one instead.”;
5. Use str_replace() over preg_replace() and ereg_replace()
While replacing strings, str_replace() is faster, almost 61% more faster than preg_replace() and ereg_replace().
But while using regular expressions, preg_replace() and ereg_replace() functions will be faster than str_replace().
6. Use Ternary Operators
Instead of always using the usual if and else statements, use ternary
operators. Ternary operators not only free space, but it also makes
codes neat, stripped, and clear.
When you use more ternary operators for a single statement, as PHP gets confused what to do next.
Here’s an example of ternary operator.
$dothis = (empty($_POST[‘dothis’]))? ‘default’ : $_POST [‘dothis’];
Which is as identical as the following if and else statement.
if(empty($_POST[‘dothis’])){
$action = ‘default’;
}else{
$action = $_POST[‘dothis’];
}
7. Use Memcached for Database caching
Memcached is an excellent way to cache your database. While there are
many caching options available, Memcached seems to always top the list
as the most efficient way to cache database.
It’s not easy to implement the caching system in the first place, but
if you’re using PHP and MySQL to build a website, then Memcached might
be the answer. Memcached will certainly help speed up the process by
caching up web page and script, thereby serving those scripts from
Memcached Server rather than the actual server. The web page request, as
a result, becomes faster
8. Use Frameworks
Use PHP Frameworks whenever possible. Fameworks like Zend,
CakePHP, Symphony, and CodeIgniter can greatly reduce the time you spend
while developing a site.
Frameworks are software that is bundled with frequently used
utilities that help reduce the overhead while developing Web
applications.