0

I am checking some code and I am wondering if this expression could ever be false:

!isset($_POST['foo']) || $_POST['foo']

Context:

$bar = (!isset($_POST['foo']) || $_POST['foo']) ? 1 : 0;
Blackbam
  • 17,496
  • 26
  • 97
  • 150
tirenweb
  • 30,963
  • 73
  • 183
  • 303
  • 7
    If the value of `$_POST['foo']` is an empty string it would be `false` – naththedeveloper Jan 09 '19 at 15:51
  • 1
    `isset()` does not check if something is empty. Those are mostly used for radios, checkboxes, select inputs. What is it exactly that you want to check against? – Funk Forty Niner Jan 09 '19 at 15:55
  • It basically states that if `$_POST['foo']` **is not** set or if it has a value, we're all good - so, yeah it looks like it's explicitly looking for empty form fields; this may fall over with ` – CD001 Jan 09 '19 at 15:55

3 Answers3

2

According to a quick test below, it is indeed possible

$trueVar = true; // Or 1
$falseVar = false; // Or 0
$nullVar = null;
$emptyVar = "";

$result1 = (!isset($trueVar) || $trueVar) ? 1 : 0;
$result2 = (!isset($falseVar) || $falseVar) ? 1 : 0;
$result3 = (!isset($nullVar) || $nullVar) ? 1 : 0;
$result4 = (!isset($emptyVar) || $emptyVar) ? 1 : 0;

Output: 
result1 = 1
result2 = 0
result3 = 1
result4 = 0

Note: Why don't you just simply do $bar = isset($_POST['foo']) and adquire if it's empty or not?

Edit: Funk Forty Niner is correctt isset($_POST) will always return true because it's always set due to being a global variable, empty() should be used instead or another approach

https://3v4l.org/EtWn0

abr
  • 2,071
  • 22
  • 38
  • I was actually wrong about isset on **null** value, so this thread is also worth reading https://stackoverflow.com/questions/7191626/isset-and-empty-what-to-use?rq=1 – cwhsu Jan 09 '19 at 16:23
0

You could think of the following cases:

  • $_POST['foo'] doesn't exist

$bar = (true || null) ? 1 : 0 => 1

  • $_POST['foo'] exists and value is anything but 0, false or empty string

$bar = (false || true) ? 1 : 0 => 1

  • $_POST['foo'] exists and value is 0, false, empty string

$bar = (false || false) ? 1 : 0 => 0

  • $_POST['foo'] exists and value is null

$bar = (true || false) ? 1 : 0 => 1

I think you should read this In php, is 0 treated as empty?

cwhsu
  • 1,493
  • 24
  • 31
0

Problem statement:

!isset($_POST['foo']) || $_POST['foo']

Pseudo-Code with wods:

NOT isset($_POST['foo']) OR $_POST['foo']

Not has a higher binding than the OR operator (http://php.net/manual/de/language.operators.logical.php).

What you are really asking: Does isset($_POST['foo']) always evaluate to the same result as $_POST['foo']?

Definition of the isset function:

isset — Determine if a variable is set and is not NULL

http://php.net/manual/en/function.isset.php

Therefore isset only evaluates to false if a variable is not existing and not null.

Automatically casting to Boolean:

A plain variable in PHP in a boolean condition evaluates to false under the following conditions:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements the
  • special type NULL (including unset variables)
  • SimpleXML objects created from empty tags

See: http://php.net/manual/en/language.types.boolean.php . As you can see there are a lot of cases where this is not the same.

What your code means in words: If the variable is one of 0,0.0,"","0",[] or an empty XML object return 1.

Blackbam
  • 17,496
  • 26
  • 97
  • 150