$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!
$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!
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.
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.
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'] : '';