New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ

The Nullsafe operator, introduced in PHP 8.0, is a game-changer for handling nullable properties and method calls more gracefully. It allows you to avoid verbose null checks, making your code cleaner and more readable.

Example Tradition Null …


This content originally appeared on DEV Community and was authored by Irpan Abdul Rahman

The Nullsafe operator, introduced in PHP 8.0, is a game-changer for handling nullable properties and method calls more gracefully. It allows you to avoid verbose null checks, making your code cleaner and more readable.

Example Tradition Null Check

$userCountry = null;
if ($user !== null) {
   if ($user->getAddress() !== null) {
      $userCountry = $user->getAddress()->getCountry();  
   }
}

๐–๐ก๐ฒ ๐”๐ฌ๐ž ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ?

โœ… ๐‚๐จ๐ง๐œ๐ข๐ฌ๐ž๐ง๐ž๐ฌ๐ฌ: Reduces the amount of boilerplate code required for null checks.
โœ… ๐‘๐ž๐š๐๐š๐›๐ข๐ฅ๐ข๐ญ๐ฒ: Makes your code more readable and expressive, clearly showing the intent of handling nullable values.
โœ… ๐’๐š๐Ÿ๐ž๐ญ๐ฒ: Helps avoid null dereference errors in a more elegant way, ensuring your code handles potential null values seamlessly.

Nullsafe implementation

$userCountry = $user?->getAddress()?->getCountry();

Have you started using the Nullsafe operator in your PHP 8 projects? Share your thoughts and experiences!


This content originally appeared on DEV Community and was authored by Irpan Abdul Rahman


Print Share Comment Cite Upload Translate Updates
APA

Irpan Abdul Rahman | Sciencx (2024-07-06T00:59:22+00:00) New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ. Retrieved from https://www.scien.cx/2024/07/06/new-in-php-8-%f0%9d%90%92%f0%9d%90%a2%f0%9d%90%a6%f0%9d%90%a9%f0%9d%90%a5%f0%9d%90%a2%f0%9d%90%9f%f0%9d%90%b2-%f0%9d%90%98%f0%9d%90%a8%f0%9d%90%ae%f0%9d%90%ab-%f0%9d%90%82%f0%9d%90%a8%f0%9d%90%9d/

MLA
" » New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ." Irpan Abdul Rahman | Sciencx - Saturday July 6, 2024, https://www.scien.cx/2024/07/06/new-in-php-8-%f0%9d%90%92%f0%9d%90%a2%f0%9d%90%a6%f0%9d%90%a9%f0%9d%90%a5%f0%9d%90%a2%f0%9d%90%9f%f0%9d%90%b2-%f0%9d%90%98%f0%9d%90%a8%f0%9d%90%ae%f0%9d%90%ab-%f0%9d%90%82%f0%9d%90%a8%f0%9d%90%9d/
HARVARD
Irpan Abdul Rahman | Sciencx Saturday July 6, 2024 » New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ., viewed ,<https://www.scien.cx/2024/07/06/new-in-php-8-%f0%9d%90%92%f0%9d%90%a2%f0%9d%90%a6%f0%9d%90%a9%f0%9d%90%a5%f0%9d%90%a2%f0%9d%90%9f%f0%9d%90%b2-%f0%9d%90%98%f0%9d%90%a8%f0%9d%90%ae%f0%9d%90%ab-%f0%9d%90%82%f0%9d%90%a8%f0%9d%90%9d/>
VANCOUVER
Irpan Abdul Rahman | Sciencx - » New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/06/new-in-php-8-%f0%9d%90%92%f0%9d%90%a2%f0%9d%90%a6%f0%9d%90%a9%f0%9d%90%a5%f0%9d%90%a2%f0%9d%90%9f%f0%9d%90%b2-%f0%9d%90%98%f0%9d%90%a8%f0%9d%90%ae%f0%9d%90%ab-%f0%9d%90%82%f0%9d%90%a8%f0%9d%90%9d/
CHICAGO
" » New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ." Irpan Abdul Rahman | Sciencx - Accessed . https://www.scien.cx/2024/07/06/new-in-php-8-%f0%9d%90%92%f0%9d%90%a2%f0%9d%90%a6%f0%9d%90%a9%f0%9d%90%a5%f0%9d%90%a2%f0%9d%90%9f%f0%9d%90%b2-%f0%9d%90%98%f0%9d%90%a8%f0%9d%90%ae%f0%9d%90%ab-%f0%9d%90%82%f0%9d%90%a8%f0%9d%90%9d/
IEEE
" » New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ." Irpan Abdul Rahman | Sciencx [Online]. Available: https://www.scien.cx/2024/07/06/new-in-php-8-%f0%9d%90%92%f0%9d%90%a2%f0%9d%90%a6%f0%9d%90%a9%f0%9d%90%a5%f0%9d%90%a2%f0%9d%90%9f%f0%9d%90%b2-%f0%9d%90%98%f0%9d%90%a8%f0%9d%90%ae%f0%9d%90%ab-%f0%9d%90%82%f0%9d%90%a8%f0%9d%90%9d/. [Accessed: ]
rf:citation
» New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ | Irpan Abdul Rahman | Sciencx | https://www.scien.cx/2024/07/06/new-in-php-8-%f0%9d%90%92%f0%9d%90%a2%f0%9d%90%a6%f0%9d%90%a9%f0%9d%90%a5%f0%9d%90%a2%f0%9d%90%9f%f0%9d%90%b2-%f0%9d%90%98%f0%9d%90%a8%f0%9d%90%ae%f0%9d%90%ab-%f0%9d%90%82%f0%9d%90%a8%f0%9d%90%9d/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.