{"id":2058,"date":"2015-11-01T16:57:49","date_gmt":"2015-11-02T00:57:49","guid":{"rendered":"https:\/\/www.virendrachandak.com\/techtalk\/?p=2058"},"modified":"2015-11-01T16:59:00","modified_gmt":"2015-11-02T00:59:00","slug":"php-7-null-coalesce-operator","status":"publish","type":"post","link":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/","title":{"rendered":"PHP 7 &#8211; Null Coalesce Operator"},"content":{"rendered":"<p>PHP 7 brings a lot of improvements, features and new operators as compared to the earlier versions on PHP. One of the new operators is the Null Coalesce Operator (??). This operator can be used instead of using isset() along with the ternary operator (?:).<\/p>\n<p>The Null coalesce operator (??) returns the result of its first operand if it exists and is not NULL, or else it will return its second operand. Using this operator like $_GET[&#8216;mykey&#8217;] ?? &#8220;&#8221; will not raise an E_NOTICE, and can be safely used.<br \/>\n<!--more--><br \/>\nSo, instead of using isset and ternary operator (?:) we can just use the Null Coalesce Operator (??). Here is an example usage:<\/p>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n\/\/ Fetches the value of $_GET&#x5B;'user'] \r\n\/\/ and returns 'nobody' if it does not exist.\r\n$username = $_GET&#x5B;'user'] ?? 'nobody';\r\n\/\/ This is equivalent to:\r\n$username = isset($_GET&#x5B;'user']) ? $_GET&#x5B;'user'] : 'nobody';\r\n<\/pre>\n<p>We can also chain the Null Coalesce operator. Here is an example:<\/p>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n\/\/ This will return the first defined value out of \r\n\/\/ $_GET&#x5B;'user'], $_POST&#x5B;'user'], and 'nobody'.\r\n$username = $_GET&#x5B;'user'] ?? $_POST&#x5B;'user'] ?? 'nobody';\r\n<\/pre>\n<p>More examples and difference from ternary operator<\/p>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n\/\/ This will make the value of $action to be 'none'.\r\n\/\/ This is because the first operand is null.\r\n$action = null ?? 'none'; \r\n\r\n\/\/ Ternary Operator, value will be 12\r\n$action = false ?:  12;\r\n\/\/ Null Coalesce Operator, value will be false. \r\n\/\/ This is because the first operator exists and is not null.\r\n$action = false ?? 'none';\r\n<\/pre>\n<p>Here are a few more examples from <a href=\"https:\/\/wiki.php.net\/rfc\/isset_ternary\" target=\"_blank\">PHP RFP for Null Coalesce Operator<\/a><\/p>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n\/\/ Calls a hypothetical model-getting function,\r\n\/\/ and uses the provided default if it fails\r\n$model = Model::get($id) ?? $default_model;\r\n\/\/ This is equivalent to: \r\nif (($model = Model::get($id)) === NULL) { $model = $default_model; }\r\n \r\n\/\/ Parse JSON image metadata, and\r\n\/\/ if the width is missing, assume 100\r\n$imageData = json_decode(file_get_contents('php:\/\/input'));\r\n$width = $imageData&#x5B;'width'] ?? 100;\r\n\/\/ This is equivalent to:\r\n$width = isset($imageData&#x5B;'width']) ? $imageData&#x5B;'width'] : 100;\r\n<\/pre>\n<p>This example demonstrates the precedence relative to the ternary operator and the boolean or operator, which is the same as C#:<\/p>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\nvar_dump(2 ?? 3 ? 4 : 5);      \/\/ (2 ?? 3) ? 4 : 5        =&gt; int(4)\r\nvar_dump(0 || 2 ?? 3 ? 4 : 5); \/\/ ((0 || 2) ?? 3) ? 4 : 5 =&gt; int(4)\r\n<\/pre>\n<p>More reading:<br \/>\n<a href=\"https:\/\/wiki.php.net\/rfc\/isset_ternary\" target=\"_blank\">PHP RFC: Null Coalesce Operator<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP 7 brings a lot of improvements, features and new operators as compared to the earlier versions on PHP. One of the new operators is the Null Coalesce Operator (??). This operator can be used instead of using isset() along with the ternary operator (?:). The Null coalesce operator (??) returns the result of its [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[143],"tags":[149],"class_list":["post-2058","post","type-post","status-publish","format-standard","hentry","category-php","tag-php-7"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP 7 - Null Coalesce Operator - Virendra&#039;s TechTalk<\/title>\n<meta name=\"description\" content=\"The coalesce, or ??, operator is added in PHP 7, which returns the result of its first operand if it exists and is not NULL, or else its second operand.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP 7 - Null Coalesce Operator - Virendra&#039;s TechTalk\" \/>\n<meta property=\"og:description\" content=\"The coalesce, or ??, operator is added in PHP 7, which returns the result of its first operand if it exists and is not NULL, or else its second operand.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/\" \/>\n<meta property=\"og:site_name\" content=\"Virendra&#039;s TechTalk\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-02T00:57:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-11-02T00:59:00+00:00\" \/>\n<meta name=\"author\" content=\"Virendra Chandak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:site\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virendra Chandak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/\"},\"author\":{\"name\":\"Virendra Chandak\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17\"},\"headline\":\"PHP 7 &#8211; Null Coalesce Operator\",\"datePublished\":\"2015-11-02T00:57:49+00:00\",\"dateModified\":\"2015-11-02T00:59:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/\"},\"wordCount\":372,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17\"},\"keywords\":[\"PHP 7\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/\",\"url\":\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/\",\"name\":\"PHP 7 - Null Coalesce Operator - Virendra&#039;s TechTalk\",\"isPartOf\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#website\"},\"datePublished\":\"2015-11-02T00:57:49+00:00\",\"dateModified\":\"2015-11-02T00:59:00+00:00\",\"description\":\"The coalesce, or ??, operator is added in PHP 7, which returns the result of its first operand if it exists and is not NULL, or else its second operand.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"TechTalk\",\"item\":\"https:\/\/www.virendrachandak.com\/techtalk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP 7 &#8211; Null Coalesce Operator\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#website\",\"url\":\"https:\/\/www.virendrachandak.com\/techtalk\/\",\"name\":\"Virendra's TechTalk\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.virendrachandak.com\/techtalk\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17\",\"name\":\"Virendra Chandak\",\"logo\":{\"@id\":\"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/www.virendrachandak.com\",\"https:\/\/www.facebook.com\/virendrachandak\",\"https:\/\/www.linkedin.com\/in\/virendrachandak\/\",\"https:\/\/x.com\/virendrachandak\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP 7 - Null Coalesce Operator - Virendra&#039;s TechTalk","description":"The coalesce, or ??, operator is added in PHP 7, which returns the result of its first operand if it exists and is not NULL, or else its second operand.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/","og_locale":"en_US","og_type":"article","og_title":"PHP 7 - Null Coalesce Operator - Virendra&#039;s TechTalk","og_description":"The coalesce, or ??, operator is added in PHP 7, which returns the result of its first operand if it exists and is not NULL, or else its second operand.","og_url":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/","og_site_name":"Virendra&#039;s TechTalk","article_publisher":"https:\/\/www.facebook.com\/virendrachandak","article_author":"https:\/\/www.facebook.com\/virendrachandak","article_published_time":"2015-11-02T00:57:49+00:00","article_modified_time":"2015-11-02T00:59:00+00:00","author":"Virendra Chandak","twitter_card":"summary_large_image","twitter_creator":"@virendrachandak","twitter_site":"@virendrachandak","twitter_misc":{"Written by":"Virendra Chandak","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/#article","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/"},"author":{"name":"Virendra Chandak","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"headline":"PHP 7 &#8211; Null Coalesce Operator","datePublished":"2015-11-02T00:57:49+00:00","dateModified":"2015-11-02T00:59:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/"},"wordCount":372,"commentCount":0,"publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"keywords":["PHP 7"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/","url":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/","name":"PHP 7 - Null Coalesce Operator - Virendra&#039;s TechTalk","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website"},"datePublished":"2015-11-02T00:57:49+00:00","dateModified":"2015-11-02T00:59:00+00:00","description":"The coalesce, or ??, operator is added in PHP 7, which returns the result of its first operand if it exists and is not NULL, or else its second operand.","breadcrumb":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.virendrachandak.com\/techtalk\/php-7-null-coalesce-operator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"TechTalk","item":"https:\/\/www.virendrachandak.com\/techtalk\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},{"@type":"ListItem","position":3,"name":"PHP 7 &#8211; Null Coalesce Operator"}]},{"@type":"WebSite","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website","url":"https:\/\/www.virendrachandak.com\/techtalk\/","name":"Virendra's TechTalk","description":"","publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.virendrachandak.com\/techtalk\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17","name":"Virendra Chandak","logo":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.virendrachandak.com","https:\/\/www.facebook.com\/virendrachandak","https:\/\/www.linkedin.com\/in\/virendrachandak\/","https:\/\/x.com\/virendrachandak"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2vTtQ-xc","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/2058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/comments?post=2058"}],"version-history":[{"count":0,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/2058\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/media?parent=2058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/categories?post=2058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/tags?post=2058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}