"PHP 널 병합 연산자 ??"의 두 판 사이의 차이

17번째 줄: 17번째 줄:
var_dump( $prices['banana'] ?? 100 ); # int(100)
var_dump( $prices['banana'] ?? 100 ); # int(100)
var_dump( $prices['lemon'] ?? 100 ); # int(200)
var_dump( $prices['lemon'] ?? 100 ); # int(200)
</syntaxhighlight>
<syntaxhighlight lang='php' run>
$_POST = ['action' => 'edit'];
$action = $_POST['action'])) ?? 'default';
echo $action;
</syntaxhighlight>
<syntaxhighlight lang='php' run>
$_POST = [];
$action = $_POST['action'])) ?? 'default';
echo $action;
</syntaxhighlight>
</syntaxhighlight>



2021년 7월 16일 (금) 10:40 판

1 개요

PHP null coalescing operator
PHP 널 병합 연산자, PHP null 합체 연산자
??
  • PHP 버전 7에 도입되었다.
$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)
$_POST = ['action' => 'edit'];
$action = $_POST['action'])) ?? 'default';
echo $action;
$_POST = [];
$action = $_POST['action'])) ?? 'default';
echo $action;

2 같이 보기

3 참고

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