PHP isset and unset

This page is marked as In Progress so expect small errors or unfinished bits

Up to now you have used a fairly simple way to tell if something exists:

if (!$_POST['fred']) {
        

This is correct but does result in a notice/warning from the server when the value is not set. This is because you have tried to access a variable which does not exist. More correct is to use isset.

ISSET

This checks whether a variable has ever been allocated any value:

if (isset($fred)) {
        

If $fred exists then the test succeeds.

UNSET

Traditionally if you want to clear a variable you would set it to an empty string:

$fred="";
        

That is not good enough any more because ISSET will still see it (it still exists but is empty). Instead you should destroy the variable totally:

unset($fred);
        
submit to reddit Delicious Tweet