<center>  </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/>
author | gentlemanoi | ||||||
---|---|---|---|---|---|---|---|
permlink | php7-how-to-use-null-coalescing-operator | ||||||
category | utopian-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}} | ||||||
created | 2018-02-08 05:44:15 | ||||||
last_update | 2018-02-08 06:56:33 | ||||||
depth | 0 | ||||||
children | 5 | ||||||
last_payout | 2018-02-15 05:44:15 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 20.280 HBD | ||||||
curator_payout_value | 8.727 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 3,748 | ||||||
author_reputation | 5,059,544,469,757 | ||||||
root_title | "PHP7 : How to use Null Coalescing Operator" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 35,837,605 | ||||||
net_rshares | 5,237,260,942,366 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
mr-anderson | 0 | 526,299,234 | 100% | ||
j3dy | 0 | 11,585,051,065 | 100% | ||
droucil | 0 | 35,999,406,391 | 100% | ||
st3llar | 0 | 610,083,041 | 3% | ||
utopian-io | 0 | 5,156,676,765,338 | 3.69% | ||
teeheecakesph | 0 | 2,287,457,046 | 100% | ||
wens | 0 | 10,118,056,412 | 100% | ||
azwarrangkuti | 0 | 5,436,376,087 | 100% | ||
yorkchinese | 0 | 6,829,397,385 | 90% | ||
steemitstats | 0 | 2,050,553,892 | 5% | ||
gameon | 0 | 2,482,781,849 | 100% | ||
hunterx3 | 0 | 1,644,065,371 | 100% | ||
gentlemanoi | 0 | 556,882,651 | 100% | ||
hirarin | 0 | 457,766,604 | 100% | ||
gaottantacinque | 0 | 0 | 100% |
Nice article :) https://steemit.com/coding/@gaottantacinque/rasmus-lerdorf-php-inventor-in-milan
author | gaottantacinque |
---|---|
permlink | re-gentlemanoi-php7-how-to-use-null-coalescing-operator-20180524t230843932z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"links":["https://steemit.com/coding/@gaottantacinque/rasmus-lerdorf-php-inventor-in-milan"],"app":"steemit/0.1"} |
created | 2018-05-24 23:08:42 |
last_update | 2018-05-24 23:08:42 |
depth | 1 |
children | 0 |
last_payout | 2018-05-31 23:08:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 97 |
author_reputation | 13,463,378,539,470 |
root_title | "PHP7 : How to use Null Coalescing Operator" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 57,537,761 |
net_rshares | 0 |
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)**
author | roj |
---|---|
permlink | re-gentlemanoi-php7-how-to-use-null-coalescing-operator-20180208t070052460z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-02-08 07:00:54 |
last_update | 2018-02-08 07:03:21 |
depth | 1 |
children | 1 |
last_payout | 2018-02-15 07:00:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 549 |
author_reputation | 12,636,295,215,793 |
root_title | "PHP7 : How to use Null Coalescing Operator" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 35,850,872 |
net_rshares | 0 |
I'll do it next time. Thank you so much.
author | gentlemanoi |
---|---|
permlink | re-roj-re-gentlemanoi-php7-how-to-use-null-coalescing-operator-20180208t071120381z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-02-08 07:11:27 |
last_update | 2018-02-08 07:11:27 |
depth | 2 |
children | 0 |
last_payout | 2018-02-15 07:11:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 40 |
author_reputation | 5,059,544,469,757 |
root_title | "PHP7 : How to use Null Coalescing Operator" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 35,852,572 |
net_rshares | 0 |
@gentlemanoi, No matter approved or not, I upvote and support you.
author | steemitstats |
---|---|
permlink | 20180208t054527787z-post |
category | utopian-io |
json_metadata | {"tags":["utopian-io"]} |
created | 2018-02-08 05:45:06 |
last_update | 2018-02-08 05:45:06 |
depth | 1 |
children | 0 |
last_payout | 2018-02-15 05:45:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 66 |
author_reputation | 351,882,871,185 |
root_title | "PHP7 : How to use Null Coalescing Operator" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 35,837,760 |
net_rshares | 0 |
### 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> [](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**
author | utopian-io |
---|---|
permlink | re-gentlemanoi-php7-how-to-use-null-coalescing-operator-20180209t074442892z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-02-09 07:44:42 |
last_update | 2018-02-09 07:44:42 |
depth | 1 |
children | 0 |
last_payout | 2018-02-16 07:44:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,509 |
author_reputation | 152,955,367,999,756 |
root_title | "PHP7 : How to use Null Coalescing Operator" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 36,106,620 |
net_rshares | 0 |