0
$n = isset($_GET["n"]) ? $_GET['n'] : '';

I find this "method" to avoid errors before insert stuff in the input type.. and it works.. but I would like a detailed explanation of this line. Thank you!

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • It's called a [Terniary operator](https://www.php.net/manual/en/language.operators.comparison.php). Think of it as a one line if statement. – Gavin Sep 22 '19 at 10:55

2 Answers2

0

This is called the ternary operator shorthand of if...else

(condition) ? true : false

There is a condition which has been checked on left, if its true the statement after the ? will be execute else the statement after the : will execute.

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

It's called Ternary operator

The ternary operator is a shorthand for the if {} else {} structure. Instead of writing this:

if ($condition) {
    $result = 'foo' 
} else {
    $result = 'bar'
}

You can write this:

$result = $condition ? 'foo' : 'bar';

If this $condition evaluates to true, the lefthand operand will be assigned to $result. If the condition evaluates to false, the righthand will be used.

In your case

If the value of $_GET["n"] isset then it'll take $_GET["n"] value.

if the value is not set then it'll take ('') value.

$n = isset($_GET["n"]) ? $_GET['n'] : '';
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49