PHP Variable safety - to avoid Notices
Simple IF statements are often used, especially in Drupal Theme-ing. ;They look like this
<?php
if ($variable) {
//do some cool stuff with that variable here
}
?>
The problem is that if $variable is undefined, php throws a Notice which takes time to throw, and time to log.
Safe handling of variable avoids this and ends up being faster on the php side.
Use empty () or !empty() to check for the $variable and no Notice gets thrown :
<?php
if (!empty($variable)) {
//do some cool stuff with that variable here
}
?>
If you must check for some other value of that variable, make it avoid throwing a Notice by making it an AND structured so that if the item is empty, it will never get to the comparison:
<?php
if (!empty($variable) && ($variable > 5)) {
//do some cool stuff with that variable here
}
?>