Wednesday, December 15, 2004

Deepu.S.Nath Php Tips

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:

error_reporting(E_ALL);

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'];

and

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.

Regards

Deepu.S.Nath

No comments: