0

In my case, the variables $nr and $str can have only two possible values. $nr can contain integer/numeric or null, not "null" between double quotes. $str can contain a string value or null. Can I just check if the variables are empty or not like this?

if ($nr) {}
if (! $nr) { return; }
if ($str) {}
if (! $str) { return; }

I think the answer is Yes but some code snippets of others made me doubt about that because they check it as following.

if (empty($var) || "" === $var || null === $var) { return; }}

I think this is unnecessary. To be specific, it is in PHP7.4.

10
  • either they specifically means something with this conditions or they are completely noob. Commented Feb 9, 2021 at 9:48
  • you can read on php.net about what values are checked by empty, that should be sufficient for your use case. Commented Feb 9, 2021 at 9:48
  • What have you tried? Where are you stuck? Can you share sample data that is not covered by the given check? Commented Feb 9, 2021 at 9:49
  • It's not clear what you really want to achieve with this condition. You say "empty", but I'm not sure you really mean empty. 0 is considered empty and falsey, as well as an empty string. Do you want to detect those or only the null values? Commented Feb 9, 2021 at 9:51
  • 1
    This, however, is completely redundant: if (empty($var) || "" === $var || null === $var) because empty already covers those cases. Commented Feb 9, 2021 at 9:53

2 Answers 2

3

You can check https://www.php.net/manual/en/types.comparisons.php to see what can be used to check if something is truthy or falsy. Below is the table of the link before condensed into a table of things you can plug into an if statement and what the results will be.

Before utilizing these tables, it's important to understand types and their meanings. For example, "42" is a string while 42 is an int. false is a bool while "false" is a string.

Expression bool : if($x)
$x = ""; false
$x = null; false
var $x; false
$x is undefined false
$x = array(); false
$x = array('a', 'b'); true
$x = false; false
$x = true; true
$x = 1; true
$x = 42; true
$x = 0; false
$x = -1; true
$x = "1"; true
$x = "0"; false
$x = "-1"; true
$x = "php"; true
$x = "true"; true
$x = "false"; true
Sign up to request clarification or add additional context in comments.

2 Comments

Reminds me of this one, regarding the special case of "0"...
yup, automatic typecasting can trip you up. There are so many edge cases, I tend to just work with them but not conciously thinking about them. after 15 years you kinda just roll with it
1

if ($var)/if (!$var) simply check for truthy/falsey values respectively (they're complimentary opposites, which one you choose is merely a question of which makes more sense in your flow). Falsey values in PHP are:

  • false
  • null
  • 0, -0, 0.0, -0.0
  • "0" (0 as a string)
  • "" (empty string)
  • array() (empty array)
  • empty SimpleXML objects (interesting special case; thanks Obama! 🤔)

Everything else is truthy.

So, if none of your desired values fall into this list and all of your undesired values are in this list, then a simple if ($var)/if (!$var) will do just fine. If your desired/undesired list does not happen to align with this and you want some of column A but also some of column B, then you need to do more specific and complicated checks.

The only time you'll want to use emtpy is if the variable you're checking may legitimately not exist. empty($var) is just !$var, but doesn't raise an error if $var doesn't exist at all. That's its only purpose, and you do not want to suppress error reporting unless you have to. In a properly written program where you declare your variables properly, there should be very very little use for empty, since you should know what variables exist and which don't.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.