Test Multiple Variables With isset

I caught a Tweet from Taylor Otwell referencing a PR on the Laravel framework where he and others including myself were surprised to learn you can pass multiple variables to php’s isset function.

Instead of:

if (isset($a) && isset($b) {
// do stuff

You can simply do:

if (isset($a, $b)) {
// do stuff

I think this is a reflection of the fact we may know a function’s purpose but don’t necessarily fully read and absorb all of the docs. In fact, the manual points this out fairly clearly and even provides the following example:

// In the next examples we'll use var_dump to output
// the return value of isset().

$a = "test";
$b = "anothertest";

var_dump(isset($a));      // TRUE
var_dump(isset($a, $b)); // TRUE

Like they say, TMYK.

Leave a Comment

Your email address will not be published.