Some PHP Guidelines to Live By
” and ‘ Are Very Different
I see a lot of this:
$name = “Bill”;
That isn’t efficient; the whole point of double quotes is to allow interpolation. Meaning, PHP checks anything within double quotes for a variable. Like so:
echo “My name is $name”;
If you do:
$name = “Bill”;
Then PHP checks the contents of the double quotes for a variable to interpolate. This makes your scripts slower. In small scripts this isn’t noticeable — it isn’t a big difference — but in large, complex scripts, this extra work for PHP can be very noticeable. Get into the habit of doing:
$name = ‘Bill’;
“Hey, PHP, I’m not using double quotes, I’m using single quotes here, don’t even bother checking for something to interpolate.”
This reduces the work PHP has to do to parse your script, increasing efficiency, especially in larger scripts. Get into the habit — use quotes correctly.
These rules apply to everything, including echo, functions, strings… everything!
SetCookie(’name’, ‘Bill’);
Not:
SetCookie(”name”, “Bill”);
Another thing you can do is use single quotes even when using a variable, like so:
$name = ‘Bill’;
echo ‘My name is ‘.$name;
Apparently, PHP scripts parse faster this way. One other advantage of this is:
echo “<a xhref=”http://www.evolt.org” mce_href=”http://www.evolt.org”>Evolt</a>”;
Look familiar? Use single quotes and you don’t have to escape double quotes.
echo ‘<a xhref=”http://www.evolt.org” mce_href=”http://www.evolt.org”>Evolt</a>’;
Works fine, and saves you having to escape those annoying quotes. Try and get used to it. I wish I’d have been told that when I started with PHP.
Jump From PHP Mode
I also see a lot of this:
<?php
$name = ‘Bill’;
echo “<table align=”center”><tr><td>My name is $name</td></tr></table>”;
?>
No, don’t do that. One of the great features of PHP is its ability to jump in and out of PHP mode. It has actually been shown that jumping from PHP mode when outputting data actually increases your script’s parse speed, so your script will be faster. Do this:
<?php
$name = ‘Bill’;
?>
<table align=”center”><tr><td>My name is <?=$name?></td></tr></table>
Leave “PHP Mode” and print out the content, if you need to use more PHP code, just jump back in! It’s easy to do, it makes content easier to output, and it speeds up your scripts.
<?=$name?>
This is a shorthand in PHP for:
<?php echo $name; ?>
Jump into PHP mode, echo out the data contained in the variable then jump back out of PHP mode. Try and use it. It makes scripts easier to read, easier to edit and it’s just a lot easier and efficient.
register_globals Off
Another mistake I see quite often is people writing scripts that will not work if register_globals is off. In PHP 4.2, register_globals is off by default, so you need to start writing your scripts with this in mind.
Having register_globals off is no big deal. It simply means writing:
$_POST[’username’]
Rather than:
$username
It’s also a lot more secure, as it stops users being able to pass variables to your script through the query string and the like. If they stuck:
?name=Jester
Into their address bar, “Jester” would not be available in $name. It would be available in $_GET[’name’] or $HTTP_GET_VARS[’name’]. So they can’t pass variables that could twist the behaviour of your scripts.
If you have register_globals off, ensure that you have track_vars on. Track_vars makes all environment variables available in the arrays: $HTTP_SERVER_VARS ($_SERVER also, for newer versions of PHP), $HTTP_COOKIE_VARS, $HTTP_POST_VARS ($_POST also) and $HTTP_GET_VARS {$_GET also). Using these arrays instead of the normal variables isn’t much extra work for more security.
Sessions in PHP also perform a lot better with register_globals off. Take a look at this snippet of code:
$username = $_POST[’uname’]; session_register(’username’);
To register a session variable, many people use this method (I have in the past). You don’t need to use this method if you have register_globals off:
$_SESSION[’username’] = $_POST[’uname’];
In the first coding example, PHP needs to know that the variable is being registered as a session variable. In the second, we are using the $_SESSION array — PHP knows if we store something in this that it is a session variable so we don’t need to “register” it.
We can unset the username session variable like this:
unset($_SESSION[’username’]);
With MySQL
Another mistake a lot of people make is when they extract data from a database. For anyone familiar with PHP and MySQL, you will be familiar with the following function:
“mysql_fetch_array() is an extended version of mysql_fetch_row(). In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.”
The above is taken from the function description on php.net. So basically when we use mysql_fetch_array(), we are fetching two arrays: one containing indices, and one containing associative references to the field names in your database. Why? If you want indices, you will use mysql_fetch_row(), right?
We use mysql_fetch_array() when we want the associative references. It’s a lot easier when using associative references, yet we have this spare array taking up space and diminishing the efficiency of our code. Luckily those nice people at PHP have an array that fetches an associative array, and only that:
mysql_fetch_assoc() fetches an associative array. If you want only that, use this function. If you want indices, use mysql_fetch_row(). If for some reason you need both, then you should use mysql_fetch_array(). Don’t assign variables you won’t use.
