-1

Can someone explain to me what this PHP line is doing?

$fileName = (isset($_POST[self::$PARAM_FILE_NAME])) ? $_POST[self::$PARAM_FILE_NAME] : null;
user77005
  • 1,501
  • 4
  • 17
  • 25

2 Answers2

0
$fileName = (isset($_POST[self::$PARAM_FILE_NAME])) ? $_POST[self::$PARAM_FILE_NAME] : null;

It sets the variable named $fileName to either the value of $_POST[self::$PARAM_FILE_NAME] or to null. Another way to write it is:

if (isset($_POST[self::$PARAM_FILE_NAME]))
    $fileName = $_POST[self::$PARAM_FILE_NAME];
else
    $fileName = null;

This avoids a warning if the key in $_POST isn't set, which would you get with the more straightforward version:

$fileName = $_POST[self::$PARAM_FILE_NAME];
Tim
  • 4,189
  • 3
  • 23
  • 29
0

That line is simply a shorthand php if|else statement.

Expanded, it would look like this:

if(isset($_POST[self::$PARAM_FILE_NAME])) {
    $fileName = $_POST[self::$PARAM_FILE_NAME];
} else {
    $fileName = null;
}

You can read more about it here.

It is basically a shorter assigning of the variable.

scrowler
  • 23,965
  • 9
  • 60
  • 90
Darren
  • 12,924
  • 4
  • 37
  • 76