-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,769
  • 4
  • 18
  • 26
  • 1
    if the $PARAM_FILE_NAME parameter has been set by POST request, assign its value to $fileName else assign null to $fileName – Satya Nov 25 '14 at 03:53
  • http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary <-- the ternary operator. Example in the docs is equivalent to yours. – Michael Berkowski Nov 25 '14 at 03:53
  • https://stackoverflow.com/questions/889373/quick-php-syntax-question – Felix Kling Nov 25 '14 at 03:56

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,999
  • 3
  • 24
  • 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
  • 24,273
  • 9
  • 60
  • 92
Darren
  • 13,050
  • 4
  • 41
  • 79