4

In JavaScript, one can check myObject.key to see wether the value is there (not undefined) and truthy.

if (myObject.key) ...

Is there a PHP equivalent? Or do I have to keep writing code such as isset(my_array['key']) && my_array['key'] to acheive the desired result? I feel it violates DRY and looks ugly.

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • 3
    [`!empty(my_array['key'])` - http://php.net/manual/en/function.empty.php](http://php.net/manual/en/function.empty.php)? – Sean Nov 15 '15 at 21:25
  • Just to put my two cents in: https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/. Additionally, a similar [question has been asked](http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty) here on SO as well. – Jan Nov 15 '15 at 21:26
  • @Sean, write your comment as an answer. – Gino Pane Nov 15 '15 at 21:29
  • @Sean Looks awesome. `empty` checks for 'falsyness', so negating it checks for 'truthyness', am I right? And this won't raise a notice if the key is not set? – Aviv Cohn Nov 15 '15 at 21:31
  • @GinoPane I am more inclined to close a duplicate of [isset vs empty vs is_null](http://stackoverflow.com/questions/12375833/isset-vs-empty-vs-is-null) then to provide an answer. – Sean Nov 15 '15 at 21:32
  • @AvivCohn yes, per the docs - [`empty() does not generate a warning if the variable does not exist`](http://php.net/manual/en/function.empty.php) – Sean Nov 15 '15 at 21:34

2 Answers2

15

There are three "levels" of checking in php:

  1. array_key_exists($key, $array) - http://php.net/manual/en/function.array-key-exists.php

Checks if a key exists in an array. Returns true even if the value is null

  1. isset($array[$key]) - http://php.net/manual/en/function.isset.php

Check if a key exists and is not null - Returns true if the value exists and is not null, but it can be bool(false), int(0), string "" and so on.

  1. !empty($array[$key]) - http://php.net/manual/en/function.empty.php

Check if a key exists and is truthy, returns false for bool(false), int(0), non-existant keys and null, but true for bool(true), int(1), int(-1), string with length > 0 and so on. Actually the function it is empty(), but often used negated, too. This should be what you are searching for.

clemens321
  • 2,103
  • 12
  • 18
  • Exactly, something to check for existence and truthyness. Thanks :) will accept. – Aviv Cohn Nov 15 '15 at 21:34
  • Empty is your best option here, because it will not complain if you pass it non-existant keys. – reggie Dec 03 '15 at 09:25
  • #2 is incorrect. `isset` returns false if the value is `null`. https://www.php.net/manual/en/function.isset.php – Sean the Bean Jan 22 '20 at 18:36
  • @Sean the bean: exactly what I said, "isset() returns true If key exists and is not null" => false if key does not exist, false if value is null, but true if it is e.g. 0, "", false, [] – clemens321 Jan 22 '20 at 21:12
  • @clemens321 Ah, right you are. I just got mixed up because of the indentation. – Sean the Bean Jan 28 '20 at 23:08
-2

You could use the error suppression operator @:

if (@$_GET['fantasyvar']) {
    //$_GET['fantasyvar'] is set and truthy
}

Note: it's not good style and will make debugging more difficult for you.

reggie
  • 3,523
  • 14
  • 62
  • 97