Skip to content

Instantly share code, notes, and snippets.

@efuller
Last active November 13, 2022 10:38
Show Gist options
  • Save efuller/7a68851e6faddd8cf68f77b73984113d to your computer and use it in GitHub Desktop.
Save efuller/7a68851e6faddd8cf68f77b73984113d to your computer and use it in GitHub Desktop.
PHP Array isset and empty

https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
http://www.zomeoff.com/php-fast-way-to-determine-a-key-elements-existance-in-an-array/

isset

  • This will check to see if there is a value for an item in an array.
  • This will return true if the value is set to ''.
  • This will return false if the value is set to null.
$people = array(
	'one'   => 'Eric',
	'two'   => '',
	'three' => null,
	'four'  => 0,
	'five'  => '0',
	'six'   => ' ',
);

if ( isset( $people['one'] ) ) {}; // true
if ( isset( $people['two'] ) ) {}; // true
if ( isset( $people['three'] ) ) {}; // false
if ( isset( $people['four'] ) ) {}; // true
if ( isset( $people['five'] ) ) {}; // true
if ( isset( $people['six'] ) ) {}; // true

Only returns false if the value is set to null.

empty

  • Will return true if set to '', or null
  • These are values empty will evaluate as empty.
    • "" (an empty string)
    • 0 (0 as an integer)
    • 0.0 (0 as a float)
    • "0" (0 as a string)
    • null
    • false
    • array() (an empty array)
    • var $var; (a variable declared, but without a value in a class)
$people = array(
	'one'   => 'Eric',
	'two'   => '',
	'three' => null,
	'four'  => 0,
	'five'  => '0',
	'six'   => ' ',
);

if ( empty( $people['one'] ) ) {}; // false
if ( empty( $people['two'] ) ) {}; // true
if ( empty( $people['three'] ) ) {}; // true
if ( empty( $people['four'] ) ) {}; // true
if ( empty( $people['five'] ) ) {}; // true
if ( empty( $people['six'] ) ) {}; // false

Takeaways

So the big difference here is that empty will also return true if the value is ''. This makes it the more safe function to use if you want to make sure '' isn't accepted.

When checking a value directly

$people = array(
	'one'   => 'Eric',
	'two'   => '',
	'three' => null,
	'four'  => 0,
	'five'  => '0',
	'six'   => ' ',
);

if ( $people['one'] ) {}; // true
if ( $people['two'] ) {}; // false
if ( $people['three'] ) {}; // false
if ( $people['four'] ) {}; // false
if ( $people['five'] ) {}; // false
if ( $people['six'] ) {}; // true

Overview

isset checks if something exists, so you can check isset( $something['key'] ), where as empty checks if it is empty. empty( $something['key'] ) would throw an error if $something['key'] wasn't set/didn't exists. You can use if ( $var ) over if ( ! empty( $var ) ).

@colorful-tones
Copy link

Adding in comments from @bradp

isset checks if something exists. so you check isset( $something['key'] ), whereas empty checks if it is empty. empty( $something['key'] ) would thrown an error if $something['key'] wasn’t set/didn’t exist

no use in checking if ( ! empty( $var ) ) {}, when you can just do if ( $var ) {}

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