create account

PHP7 : How to use Null Coalescing Operator by gentlemanoi

View this thread on: hive.blogpeakd.comecency.com
· @gentlemanoi · (edited)
$29.01
PHP7 : How to use Null Coalescing Operator
<center>
![codeigniter-logo.png](https://s3.amazonaws.com/assets.doesnotscale.com/blog/2016/08/php_500x326-1471131047927.png)
</center>

#### What Will I Learn?
This tutorial is all about how to use null coalescing operator in PHP7.

- What is null coalescing operator
- How to use null coalescing operator
- What is the difference between null coalescing and ternary operator

#### Requirements
- PHP 7 Server
- Basic knowledge about PHP

#### Difficulty
- Basic

#### Tutorial Contents
###### What is Null Coalescing operator?
The null coalescing operator (??) will check if the first operand is set or not NULL. If it is not NULL, then it will return the value of the first operand. Otherwise, it will return the value of the second operand. 

###### How to use null coalescing operator?
This is the syntax for null coalescing operator:
```
$c = $a ?? $b;
```
Example:
```
<?php 
     $a = 'String A';
     $b = 'String B';
     $c = $a ?? $b;
     echo $c;
?>
```
This example will output 'String A' because variable `$a` has a value.

Another Examples:
```
<?php
     $a = NULL;
     $b = 'String B';
     $c = $a ?? $b;
     echo $c; // This will output 'String B';
?>
```
```
<?php
     $b = 'String B';
     $c = $a ?? $b;
     echo $c; // This will output 'String B';
?>
```
But if variable `$a` is equal to NULL or UNDEFINED, then it will output 'String B'.
```
<?php
     $a = '';
     $b = 'String B';
     $c = $a ?? $b;
     echo $c; // This will output an empty string;
?>
```
But if you set variable `$a` into an empty string, then it will return variable `$a` (empty string).
````
<?php
     $d = 'String D';
     $e = $a ?? $b ?? $c ?? $d;
?>
````
You can also have multiple null coalescing operator in one statement. So in this example, it will output 'String D' because all the previous variables are UNDEFINED.

###### What is the difference between null coalescing and ternary operator?
The difference between this two operators is that the null coalescing operator evaluates the first operand if it is null. It's like using the `isset()` function. While the ternary operator evaluates the first operand as a boolean expression.
Examples:
```
<?php

$a = '';
$b = 'String B';
echo $a ?? $b; // null coalescing operator
echo $a ?: $b; // ternarty operator

?>
```
For an empty string, the null coalescing operator will output an empty string because variable `$a` is not null. While the ternary operator will output 'String B' because empty string is false.
```
<?php

$b = 'String B';
echo $a ?? $b; // null coalescing operator
echo $a ?: $b; // ternarty operator

?>
```
For UNDEFINED variable, the null coalescing operator will output 'String B'. While the ternary operator will throw an undefined variable error.
```
<?php

$b = false; // same with 0
echo $a ?? $b; // null coalescing operator
echo $a ?: $b; // ternarty operator

?>
```
For false/0 value, the null coalescing operator will return the exact value (false/0). While the ternary operator will output 'String B'.

So, the difference between the two is that the null coalescing operator will handle the NULL and UNDEFINED variables. While the ternary operator is th shorthand for if-else. Therefore, Null Coalescing operator is not meant to replace the ternary operator.

#### Curriculum
- [PHP : How to use ZipArchive class](https://utopian.io/utopian-io/@gentlemanoi/php-how-to-use-ziparchive-class)
- [PHP : How to check if a string contains a specific word/string](https://utopian.io/utopian-io/@gentlemanoi/php-how-to-check-if-a-string-contains-a-specific-word-string)
    

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@gentlemanoi/php7-how-to-use-null-coalescing-operator">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , , ,
properties (23)
authorgentlemanoi
permlinkphp7-how-to-use-null-coalescing-operator
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":1903522,"name":"php-src","full_name":"php/php-src","html_url":"https://github.com/php/php-src","fork":false,"owner":{"login":"php"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","tutorial","php7"],"users":["gentlemanoi"],"links":["https://s3.amazonaws.com/assets.doesnotscale.com/blog/2016/08/php_500x326-1471131047927.png","https://utopian.io/utopian-io/@gentlemanoi/php-how-to-use-ziparchive-class","https://utopian.io/utopian-io/@gentlemanoi/php-how-to-check-if-a-string-contains-a-specific-word-string"],"image":["https://s3.amazonaws.com/assets.doesnotscale.com/blog/2016/08/php_500x326-1471131047927.png"],"moderator":{"account":"roj","time":"2018-02-08T06:56:29.376Z","reviewed":true,"pending":false,"flagged":false}}
created2018-02-08 05:44:15
last_update2018-02-08 06:56:33
depth0
children5
last_payout2018-02-15 05:44:15
cashout_time1969-12-31 23:59:59
total_payout_value20.280 HBD
curator_payout_value8.727 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,748
author_reputation5,059,544,469,757
root_title"PHP7 : How to use Null Coalescing Operator"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,837,605
net_rshares5,237,260,942,366
author_curate_reward""
vote details (15)
@gaottantacinque ·
Nice article :)

https://steemit.com/coding/@gaottantacinque/rasmus-lerdorf-php-inventor-in-milan
properties (22)
authorgaottantacinque
permlinkre-gentlemanoi-php7-how-to-use-null-coalescing-operator-20180524t230843932z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://steemit.com/coding/@gaottantacinque/rasmus-lerdorf-php-inventor-in-milan"],"app":"steemit/0.1"}
created2018-05-24 23:08:42
last_update2018-05-24 23:08:42
depth1
children0
last_payout2018-05-31 23:08:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length97
author_reputation13,463,378,539,470
root_title"PHP7 : How to use Null Coalescing Operator"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id57,537,761
net_rshares0
@roj · (edited)
Thank you for the contribution. It has been approved.

- I approve this contribution but there may be some improvements like:
  - You could have stated that this is a new feature came with PHP 7.
  - You could have explain its `isset()` equivalent of the feature. like:
```
$variable = isset($var1) ? $var1 : $var2;
```
  - Next time, please make sure that you provide any information you can with your simple tutorials like this.

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorroj
permlinkre-gentlemanoi-php7-how-to-use-null-coalescing-operator-20180208t070052460z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-08 07:00:54
last_update2018-02-08 07:03:21
depth1
children1
last_payout2018-02-15 07:00:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length549
author_reputation12,636,295,215,793
root_title"PHP7 : How to use Null Coalescing Operator"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,850,872
net_rshares0
@gentlemanoi ·
I'll do it next time. Thank you so much.
properties (22)
authorgentlemanoi
permlinkre-roj-re-gentlemanoi-php7-how-to-use-null-coalescing-operator-20180208t071120381z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-08 07:11:27
last_update2018-02-08 07:11:27
depth2
children0
last_payout2018-02-15 07:11:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length40
author_reputation5,059,544,469,757
root_title"PHP7 : How to use Null Coalescing Operator"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,852,572
net_rshares0
@steemitstats ·
@gentlemanoi, No matter approved or not, I upvote and support you.
properties (22)
authorsteemitstats
permlink20180208t054527787z-post
categoryutopian-io
json_metadata{"tags":["utopian-io"]}
created2018-02-08 05:45:06
last_update2018-02-08 05:45:06
depth1
children0
last_payout2018-02-15 05:45:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length66
author_reputation351,882,871,185
root_title"PHP7 : How to use Null Coalescing Operator"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id35,837,760
net_rshares0
@utopian-io ·
### Hey @gentlemanoi I am @utopian-io. I have just upvoted you!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Suggestions
- Contribute more often to get higher and higher rewards. I wish to see you often!
- Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
#### Get Noticed!
- Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-gentlemanoi-php7-how-to-use-null-coalescing-operator-20180209t074442892z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-09 07:44:42
last_update2018-02-09 07:44:42
depth1
children0
last_payout2018-02-16 07:44:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,509
author_reputation152,955,367,999,756
root_title"PHP7 : How to use Null Coalescing Operator"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id36,106,620
net_rshares0