-3
$action = isset($_GET['action']) ? $_GET['action'] : null;

Im a little unsure what the above line does? I have some vague ideas (and I know what GET does etc) but I have never come across the : "operator"(?) or the ? "operator" (?).

Thanks very much.

user2752347
  • 161
  • 4
  • 16
  • 2
    It's called the [ternary operator](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) – nishantjr Jul 05 '14 at 11:49

2 Answers2

1

it is a short syntax of writing

if(isset($_GET['action'])){
    $action = $_GET['action']
}else{
    $action = null;
}

That means you check a condition before ? mark, if result is true, you execute the part between ? and : But if condition check returns false, you execute the part after :

Ali
  • 131
  • 10
  • Thanks! Surely it would already be nulled? If it wasnt set that is? – user2752347 Jul 05 '14 at 11:52
  • If it was not set anywhere and if you are using strict PHP mode which I will recommend while you are learning it, then trying to access a variable which is not set already will generate a warning that you are trying to access a variable that doesn't exist. The best way is to always declare a variable before using it. – Ali Jul 05 '14 at 11:58
0

This means simply..

if(isset($_GET['action']))
   $action = $_GET['action'];
else
   $action = null;
Deepu Sasidharan
  • 4,763
  • 8
  • 32
  • 84