If null use other variable in one line in PHP

前端 未结 12 774
说谎
说谎 2020-12-29 01:47

Is there in PHP something similar to JavaScript\'s:

alert(test || \'Hello\');

So, when test is undefined or null we\'ll see Hello, otherwis

12条回答
  •  情深已故
    2020-12-29 02:41

    From PHP 7 onwards you can use something called a coalesce operator which does exactly what you want without the E_NOTICE that ?: triggers.

    To use it you use ?? which will check if the value on the left is set and not null.

    $arr = array($one ?? 'one?', $two ?? 'two?'); 
    

提交回复
热议问题