Are You Making These 10 PHP Mistakes?


One of the best things about PHP is that it’s a great language to just “dive into”, thanks to its wide-ranging popularity. Anyone with the ability to hit “Search” on Google can quickly create a program. However, this also lends to a major criticism of PHP: it’s almost too easy to find and reproduce bad code.

Here are 10 PHP mistakes that any programmer, regardless of skill level, might make at any given time. Some of the mistakes are very basic, but trip up even the best PHP programmer. Other mistakes are hard to spot (even with strict error reporting). But all of these mistakes have one thing in common: They’re easy to avoid.

Take the time and make sure that your PHP is secure, clean and running smoothly by checking your site for these common PHP blunders.



1. Single quotes, double quotes

It’s easy to just use double quotes when concatenating strings because it parses everything neatly without having to deal with escaping characters and using dot values. However, using single quotes has considerable performance gains, as it requires less processing.
Consider this string:

# $howdy = 'everyone';  
# $foo = 'hello $howdy';  
# $bar = "hello $howdy";
$foo outputs to “hello $howdy” and $bar gives us “hello everyone”. That’s one less step that PHP has to process. It’s a small change that can make significant gains in the performance of the code.


2. Semicolon after a While

It’s funny how one little character can create havoc in a program, without even being reported to the PHP error logs! Such as it is with semicolons and While statements.
Codeutopia has an excellent example of this little error, showing that these nasty errors don’t even get reported (even to E_ALL!), as it quietly falls into a silent loop.
  1. $i = 0;  
  2. while($i < 20); {  
  3.   //some code here  
  4.   $i++;  
  5. }  
Omit the ; after the while statement, and your code is in the clear.