开发者

testing for var that might be array/empty array/false

开发者 https://www.devze.com 2023-03-18 11:47 出处:网络
开发者_开发技巧I have a function that will perform a SELECT query from some db and return either:

开发者_开发技巧I have a function that will perform a SELECT query from some db and return either:

  • false (in case of error)
  • an empty array array(0) { }
  • an associative array

In order to test the return value of this function, is it good practice to do :

$someVar = $this->someFunction();

if ($someVar) {
    // ok, this is an associative array of result
} else {
    // $someVar = false OR $someVar is an empty array
}

Or do I have to do something like this instead :

$someVar = $this->someFunction();

if (is_array($someVar) && count($someVar) > 0) {
    // ok, this is an associative array of result
} else {
    // $someVar = false OR $someVar is an empty array
}

The first test seems to do what I want, but maybe I'm missing something that might go wrong after.

So, is it good practice test arrays like I did in my first example?


Neither.

Returning false to indicate error is fine at a very low level, but there should a layer between your low-level queries and your application code which inspects the return value and throws an exception on false. The top-level controller which invokes your code should be handling those exceptions (which aren't caught earlier) and displaying a user-friendly error page in production, or dumping debugging information in development, while logging the error.

There is absolutely no way you should be doing a three-way if/elseif/else branch in your application to inspect the return values of every single database query. This is an incredibly dated way of checking for errors.

Throw exceptions, and you can use your first form (if ($someVar)) or, better yet:

foreach ($this->someFunction() as $key => $row) {


}


http://php.net/manual/en/types.comparisons.php

An empty array evaluates to false. An array with values evaluates to true (even if all values would individually evaluate to false).

You can also check if (empty($array)) but it is redundant to do so unless you are concerned about the array variable not being set.

Finally, if the array is empty, $array == false is true, but $array === false is not.


According to the documentation on type comparisons, your first method is completely acceptable. An empty array will always evaluate to FALSE.

Documentation: http://php.net/manual/en/types.comparisons.php

That said, you may choose to handle no results and errors differently, e.g. logging the error message to a server log. In this case, you may want multiple if() conditions.


Neither. Test for all three cases:

if (false === $result) {
    die('There is an error!');
} elseif (empty($result)) {
    die('No results found');
}

foreach ($result as $foo) { ... }

If you really want to test for just two, then remember that foreach will work in an empty array. So, this will work:

if (false === $result) {
    die('There was an error!');
}

foreach ($result as $foo) { ... }

Or:

if (false === $result) {
    echo 'There was an error!';
} else {
    foreach ($result as $foo) { ... }
}

These last examples will simply give an empty page when the $result is an empty array. The first example will say that there are no results.

0

精彩评论

暂无评论...
验证码 换一张
取 消