<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/haj8a8bnm" -->

---
title: The null PHP cheatsheet : null coalescing and nullsafe...
description: PHP's null coalescing operator (??) returns the first operand if it's not null, otherwise the second operand, simplifying isset() checks. The nullsafe operator...
canonical: https://daily.dev/posts/haj8a8bnm
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: The null PHP cheatsheet : null coalescing and nullsafe operators | daily.dev
og:description: PHP's null coalescing operator (??) returns the first operand if it's not null, otherwise the second operand, simplifying isset() checks. The nullsafe operator...
og:url: https://daily.dev/posts/haj8a8bnm
og:image: https://api.daily.dev/og/posts/haJ8a8bNm.png
og:image:alt: Post cover image
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# The null PHP cheatsheet : null coalescing and nullsafe operators

**[PHP Dev](https://daily.dev/sources/phpdev)** · [@damienseguy](https://daily.dev/damienseguy) · 3 upvotes · 1 comments

## Summary

PHP's null coalescing operator (??) returns the first operand if it's not null, otherwise the second operand, simplifying isset() checks. The nullsafe operator (?->) safely accesses object properties or methods only when the object isn't null, preventing fatal errors. Both operators help write cleaner, more maintainable code with fewer null-related exceptions.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://cylab.be/blog/479/the-null-php-cheatsheet-null-coalescing-and-nullsafe-operators>

## Community discussion

Top comments from developers on daily.dev.

**@hazington** · 1 upvotes

> You forgot the null coalescing assignment operator! It's very useful if you want to set a default value.
>
> ```
> $var ??= 'test';
> // instead of
> $var = $var ?? 'test';
> ````

---

Tags: [#php](https://daily.dev/tags/php)

[View this post on daily.dev](https://daily.dev/posts/haj8a8bnm)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/haj8a8bnm","headline":"The null PHP cheatsheet : null coalescing and nullsafe operators","text":"Shared: The null PHP cheatsheet : null coalescing and nullsafe operators","url":"https://daily.dev/posts/haj8a8bnm","datePublished":"2026-02-02T09:43:28.036Z","dateModified":"2026-02-02T09:44:02.470Z","author":{"@type":"Person","name":"Damien seguy","url":"https://daily.dev/damienseguy","image":"https://media.daily.dev/image/upload/s--nPbjKqol--/f_auto,q_auto/v1698422671/avatars/avatar_G85JhYcDxAg024Omc8Rlp","description":"PHP developer passionate about deep language knowledge, testing, static analysis, and sustainable it","worksFor":{"@type":"Organization","name":"Exakat","logo":"https://media.daily.dev/image/upload/s--ibtl6fze--/f_auto/v1749051132/companies/exakat"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":43000}},"image":"https://media.daily.dev/image/upload/s--CxzD6vbw--/f_auto/v1722860399/public/Placeholder%2005","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"sharedContent":{"@type":"WebPage","url":"https://api.daily.dev/r/4ThBl57ux"},"comment":[{"@type":"Comment","text":"You forgot the null coalescing assignment operator! It’s very useful if you want to set a default value.\n$var ??= \u0026#x27;test\u0026#x27;;\n// instead of\n$var = $var ?? \u0026#x27;test\u0026#x27;;","datePublished":"2026-02-07T16:43:32.217Z","url":"https://daily.dev/posts/haJ8a8bNm#c-rbF5dFAtD","author":{"@type":"Person","name":"Erik Hazington","url":"https://daily.dev/hazington","image":"https://media.daily.dev/image/upload/s--xTiS_gI8--/f_auto,pg_1/v1793100807/avatars/avatar_6BVlF7XIev6nNnCbwCjYu?_a=BAMAMiiw0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/phpdev","name":"PHP Dev"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"PHP Dev","item":"https://daily.dev/squads/phpdev"},{"@type":"ListItem","position":3,"name":"The null PHP cheatsheet : null coalescing and nullsafe operators"}]}
```

