Common Mistakes PHP Developers Make: Misunderstanding isset( ) behaviour
×

Common Mistakes PHP Developers Make: Misunderstanding isset( ) behaviour

1119

PHP's success is largely due to how simple it is to create a web-based system thanks to this feature. But despite its simplicity, PHP has developed into a complicated language with a variety of nuances and intricacies that can vex developers and cause hours of agonizing debugging.

One such bug is isset( ), despite its name, isset( ) function does not only returns false if an item does not exist, but also returns false for null values. It may not be immediately apparent how harmful this conduct is, yet it frequently leads to issues.

Let’s understand this through an example-

For Instance-

For Example:-

$data = fetchRecordFromStorage($storage, $identifier);
if (!isset($data['key']) {
    // do something here if 'key' is not set
}
The author of this code presumably wanted to check if “key” was set in $data. But, as discussed, isset($data['key']) will also return false if $data['key'] was set, but was set to null. So, the above logic is flawed.


Another example:

if ($_POST['active']) {
   $postData = extractSomething($_POST);
}
// ...
if (!isset($postData)) {
   echo 'post not active';
}

The above code assumes that if $_POST['active'] returns true, then postData will necessarily be set, and therefore isset($postData) will return true. So, on the other hand, the above code assumes that the only way that isset($postData) will return false is if $_POST['active'] returned false as well.

But that’s not true.

As explained, isset($postData) will also return false if $postData was set to null. Therefore, it is possible for isset($postData) to return false even if $_POST['active'] returned true. So again, the above logic is flawed again.

And, if the intent in the above code really was to again check if $_POST['active'] returned true, relying on isset( ) function for this was a poor coding decision in any case. Instead, it would have been better to just recheck $_POST['active']

For Instance:-

if ($_POST['active']) {
    $postData = extractSomething($_POST);
}
// ...
if ($_POST['active']) {
    echo 'post not active';
}

But for instance, it is important to check if a variable was really set (i.e., to distinguish between a variable that wasn’t set and a variable that was set to null), the array_key_exists( ) method is a much more robust solution.

For example, we could rewrite the first of the above two examples as follows:

$data = fetchRecordFromStorage($storage, $identifier);
if (! array_key_exists('key', $data)) {
    // do this if 'key' isn't set
}

Moreover, by combining array_key_exists( ) method with get_defined_vars( ) method, we can reliably check whether a variable within the current scope has been set or not:

if (array_key_exists('var', get_defined_vars( ))) {
    // variable $var exists in current scope
}

This is how this irritable code can be understood well by developers to avoid unnecessary struggle debugging the code.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments