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

 
(같은 사용자의 중간 판 5개는 보이지 않습니다)
4번째 줄: 4번째 줄:
;<code>??</code>
;<code>??</code>
* PHP 버전 7에 도입되었다.
* PHP 버전 7에 도입되었다.
<syntaxhighlight lang='php'>
$output = $input ?? 'default';
// is a shorthand for
$output = isset($input) ? $input : 'default';
</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 lang='php' run>
<syntaxhighlight lang='php' run>
17번째 줄: 35번째 줄:
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>


==같이 보기==
==같이 보기==
{{z컬럼3|
* [[PHP null]]
* [[PHP null]]
* [[PHP isset()]]
* [[PHP empty()]]
* [[PHP empty()]]
* [[PHP 조건부 연산자]]
* [[PHP 조건부 연산자]]
* [[PHP 단축 조건부 연산자]]
* [[PHP 비교 연산자 목록]]
* [[null 병합 연산자]]
* [[null 병합 연산자]]
}}


==참고==
==참고==

2023년 11월 27일 (월) 00:31 기준 최신판

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 }}