PHP 널 병합 연산자 ??

(PHP 널 병합 연산자에서 넘어옴)

1 개요[ | ]

PHP null coalescing operator
PHP 널 병합 연산자, PHP null 합체 연산자
??
  • PHP 버전 7에 도입되었다.
$output = $input ?? 'default';
// is a shorthand for 
$output = isset($input) ? $input : 'default';

2 예시[ | ]

$_POST = ['action' => 'edit'];
$action = $_POST['action'] ?? 'default';
echo $action;
$_POST = [];
$action = $_POST['action'] ?? 'default';
echo $action;
$var1 = 'hello';
$var2 = null;
var_dump( $var1 ?? 'default' ); # string(5) "hello"
var_dump( $var2 ?? 'default' ); # string(7) "default"
var_dump( $var3 ?? 'default' ); # string(7) "default"
$prices = ['apple'=>300,'lemon'=>200];
var_dump( $prices['apple'] ?? 300 ); # int(300)
var_dump( $prices['banana'] ?? 100 ); # int(100)
var_dump( $prices['lemon'] ?? 100 ); # int(200)

3 같이 보기[ | ]

4 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}