isset PHP isset($_GET['something']) ? $_GET['something'] : ''

前端 未结 8 1436
清酒与你
清酒与你 2020-12-13 04:02

I am looking to expand on my PHP knowledge, and I came across something I am not sure what it is or how to even search for it. I am looking at php.net isset code, and I see

8条回答
  •  遥遥无期
    2020-12-13 04:55

    That's called a ternary operator and it's mainly used in place of an if-else statement.

    In the example you gave it can be used to retrieve a value from an array given isset returns true

    isset($_GET['something']) ? $_GET['something'] : ''
    

    is equivalent to

    if (isset($_GET['something'])) {
      $_GET['something'];
    } else {
      '';
    }
    

    Of course it's not much use unless you assign it to something, and possibly even assign a default value for a user submitted value.

    $username = isset($_GET['username']) ? $_GET['username'] : 'anonymous'
    

提交回复
热议问题