Skip to content

Instantly share code, notes, and snippets.

@amirbehzad
Last active December 30, 2015 03:58
Show Gist options
  • Save amirbehzad/de7be58b5d39c483e134 to your computer and use it in GitHub Desktop.
Save amirbehzad/de7be58b5d39c483e134 to your computer and use it in GitHub Desktop.
PHP: How to ensure a variable is an Array, and it is not empty
<?php
// returns True if the given $var is an Array, and it is not empty;
// otherwise, False. It does not gives an error if the given $var
// is not an Array, however, it returns False.
function isNonEmptyArray($var) {
return !empty(@reset($var));
}
@amirbehzad
Copy link
Author

This is basically a shorthand for:

is_array($var) && !empty($var)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment