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

34번째 줄: 34번째 줄:
* [[PHP empty()]]
* [[PHP empty()]]
* [[PHP 조건부 연산자]]
* [[PHP 조건부 연산자]]
* [[PHP 비교 연산자 목록]]
* [[null 병합 연산자]]
* [[null 병합 연산자]]



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

1 개요

PHP null coalescing operator
PHP 널 병합 연산자, PHP null 합체 연산자
??
  • PHP 버전 7에 도입되었다.
$_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)

2 같이 보기

3 참고

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