2 Quick PHP Tips

By David Cummings

So now you’re a proficient Web programmer dabbling in the famous open source language PHP. The only downside is that the language allows the programmer to be sloppy and decrease application performance unknowingly.

At my company, we use PHP on a daily basis and have many different styles and rules we try to employ to make our (rather large) application efficient and bug free. Let’s have a look at the two most important things I wish I knew before learning the language 3 years ago.

Turn On Error Reporting Immediately

The single most important thing I tell people who use PHP is to turn error reporting to its maximum level. Why would I want to do this? Generally the error reporting is set at a level that will hide many little things like:

  • declaring a variable ahead of time,
  • referencing a variable that isn’t available in that segment of code, or
  • using a define that isn’t set.

These factors might not seem like that big a deal — until you develop structured or object oriented programs with functions and classes. Too often, writing code with the error reporting turned up high would cost your hours as you scoured long functions that didn’t work because a variable was misspelled or not accessible.

PHP won’t tell you anything in that case – it’ll just create the new variable for you and initialize it to zero. The remedy is to put the following line at the top of every PHP document as you develop:


It simply forces the error reporting to be at its highest level. Try putting this line in other PHP programs, and more often than not you’ll receive a barrage of warning messages that identify all the potentially wrong elements of the code.

Single Quotes and Double Quotes are Very Different

I never recommend using " (double quotes) when programming with PHP. Always use ‘ (single quotes) unless you need the features of " (double quotes). You might think it’s much easier to write code as:

echo "Today is the $day of $month";

However, using single quotes forces variables to be outside the quotes; instead, you must use the period (.) to combine strings. It makes for faster coding but can be more difficult for other programmers to read. Let’s look at what would happen if we put an associative array value in the previous code:

echo "Today is the $date['day'] of $date['month']";

You would receive a parse error and it would be harder for another team member to read. Two correct ways to write that line of code would be:

echo 'Today is the ' . $date['day'] . ' of ' . $date['month'];


echo "Today is the {$date['day']} of {$date['month']}";

These might not look as pretty as the original code, but syntactically they are both correct. Additionally, I believe the first method, with single quotes, is easier to read.

The use of single and double quotes also applies to associative arrays. Consider this code:

$SESSION[team] = $SESSION["old_team"];

One main problem exists in that line of code. The associative entry team on the left side needs to have single quotes around it; otherwise, PHP will think it’s a define and give you a warning message (only if error reporting is at maximum). I would recommend that the code should look like this:

$SESSION['team'] = $SESSION['old_team'];

I wish I’d known the difference between single and double quotes as they pertain to strings when I first learned PHP.

Bonus Tip!

Getting Syntax Highlighting for PHP with Dreamweaver 4

As I was programming in PHP using Dreamweaver 4 as my text editor, I came across a handy little trick. You can fool Dreamweaver into thinking that code on the page is JavaScript, so that it applies the same syntax highlighting rules to your PHP as it would to JavaScript.

JavaScript and PHP both draw from the same languages, including C and Java, and therefore have many aspects in common. Try it out and see for yourself. Add this code to the top of a PHP document, open it in Dreamweaver 4, and go to the text editor (you can click Ctrl-Tab on your keyboard to switch views):

/* >The code to get PHP syntax highlighting with Dreamweaver
<script language="JavaScript"> (courtesy of */

What the code does is close out the most recent left bracket on the page (typically from <?php) and sets the script language to JavaScript for the remainder of the page. The code itself doesn’t affect PHP because it is commented out using the /*...*/ commenting system.

I hope these little PHP coding tips make your development time more efficient, and your resulting code more effective!

Meet the author
David is the founder of Hannon Hill Content Management Software, a growing software company that develops award-winning content management software for enterprises of all sizes.

No Reader comments


Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in Front-end, once a week, for free.