Best hierarchical php syntax for assigning a variable to values if they are set

前端 未结 1 1397
盖世英雄少女心
盖世英雄少女心 2020-12-11 06:17

I want to do this for 5 sets of parameters is this the best way to do it or is there some simpler syntax?

if(isset($_GET[\'credentials\'])) $credentials = $_         


        
相关标签:
1条回答
  • 2020-12-11 07:14

    PHP 7 introduced the The null coalescing operator (??), which you can use like this:

    $result = $var ?? 'default';
    

    This will assign default to result if:

    • $var is undefined.
    • $var is NULL

    You can also use multiple ?? operators:

    $result = $null_var ?? $undefined_var ?? 'hello' ?? 'world'; // Result: hello
    

    To answer your question, you should be doing something like:

    $credentials = $_GET['c'] ?? $_POST['c'] ?? $_POST['credentials'] ?? $_GET['credentials'];
    

    More details here and here

    0 讨论(0)
提交回复
热议问题