PHP, Shorthand, If..Else using Ternary Operators

前端 未结 2 1985
孤街浪徒
孤街浪徒 2021-01-20 18:37

Is there a oneliner for this? A nice Ternary OP?

$F_NAME = $_SESSION[\'USR\'][\'F_NAME\'];
if(isset($_POST[\'F_NAME\'])) {$F_NAME = $_POST[\'F_NAME\'];}


        
相关标签:
2条回答
  • 2021-01-20 19:04

    As Ghost response, or even shorter

    $F_NAME = $_POST['F_NAME'] ? : $_SESSION['USR']['F_NAME'];
    
    0 讨论(0)
  • 2021-01-20 19:09

    Its supposed to be:

    (conditions) ? true : false
       satisfies <--^      ^----> did not satisfy
    

    So this equates into:

    $F_NAME = isset($_POST['F_NAME']) ? $_POST['F_NAME'] : $_SESSION['USR']['F_NAME'];
    
    0 讨论(0)
提交回复
热议问题