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

前端 未结 8 1430
清酒与你
清酒与你 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:39

    It is called the ternary operator. It is shorthand for an if-else block. See here for an example http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

    0 讨论(0)
  • 2020-12-13 04:39

    ? is called Ternary (conditional) operator : example

    0 讨论(0)
  • 2020-12-13 04:41

    What you're looking at is called a Ternary Operator, and you can find the PHP implementation here. It's an if else statement.

    if (isset($_GET['something']) == true) {
        thing = isset($_GET['something']);
    } else {
        thing = "";
    }
    
    0 讨论(0)
  • 2020-12-13 04:46

    From php 7 you can write it even shorter:

    $age = $_GET['age']) ?? 27;
    

    And this means if age param is provided in the url it will be set to $age var, or will default to 27

    See all new features of php 7

    0 讨论(0)
  • 2020-12-13 04:48

    If you want an empty string default then a preferred way is one of these (depending on your need):

    $str_value = strval($_GET['something']);
    $trimmed_value = trim($_GET['something']);
    $int_value = intval($_GET['somenumber']);
    

    If the url parameter something doesn't exist in the url then $_GET['something'] will return null

    strval($_GET['something']) -> strval(null) -> ""

    and your variable $value is set to an empty string.

    • trim() might be prefered over strval() depending on code (e.g. a Name parameter might want to use it)
    • intval() if only numeric values are expected and the default is zero. intval(null) -> 0

    Cases to consider:

    ...&something=value1&key2=value2 (typical)

    ...&key2=value2 (parameter missing from url $_GET will return null for it)

    ...&something=+++&key2=value (parameter is " ")

    Why this is a preferred approach:

    • It fits neatly on one line and is clear what's going on.
    • It's readable than $value = isset($_GET['something']) ? $_GET['something'] : '';
    • Lower risk of copy/paste mistake or a typo: $value=isset($_GET['something'])?$_GET['somthing']:'';
    • It's compatible with older and newer php.

    Update Strict mode may require something like this:

    $str_value = strval(@$_GET['something']);
    $trimmed_value = trim(@$_GET['something']);
    $int_value = intval(@$_GET['somenumber']);
    
    0 讨论(0)
  • 2020-12-13 04:54

    You have encountered the ternary operator. It's purpose is that of a basic if-else statement. The following pieces of code do the same thing.

    Ternary:

    $something = isset($_GET['something']) ? $_GET['something'] : "failed";
    

    If-else:

    if (isset($_GET['something'])) {
        $something = $_GET['something'];
    } else {
        $something = "failed";
    }
    
    0 讨论(0)
提交回复
热议问题