Here's an awesome PHP trick that I learned recently that has saved me a lot of time!

Usually, when working with form data, one has to test to see if a POST (or GET) variable has been set before testing to see if it is equal to some value or otherwise using it in a program.

Example:

if( isset($_POST["fname"]) && ( $fname = $_POST["fname"] ) == "John" )
//Do Stuff with $fname

Isn't that terribly long? The truth is that you can do it much more easily by:


if( ( $fname = &$_POST["fname" ]) == "John" )
//Do Stuff with $fname

This trick has saved me a lot of typing, and if the POST variable doesn't exist, $fname will have a value of NULL.

The reason that you need the '&' is because PHP gives nasty error messages if it can't find an array element, and asking for the reference of a non-existent element returns NULL without errors.