Null Coalescing in PHP Snippet and Tutorial
In this tutorial, you will learn how to use Null Coalescing using PHP Language. The tutorial aims to provide IT/CS students and new programmers with a reference for learning the different techniques and usage of the PHP built-in methods and operators. Here, snippets are provided for better understanding, and a sample source code zip file is also provided and is free to download.
What is a Null Coalescing operator?
Null Coalescing is also known as the Logical Defined-Or Operator in Perl. It is a binary operator of the syntax for basic conditional expressions in most programming languages. Using this operator, you can evaluate if the given variables or constants are set or not null and return the first not null and set instance.
How to implement Null Coalescing in PHP?
In PHP, the old way of doing this is using the conditional shorthand if statement which requires you to check first if the given variable or constant is existing/set and not null. In PHP version 7 up to the latest, PHP comes with the ?? or the Null Coalescing Operator. This operator automatically evaluates the variables and constants. It will return the first instance that is set and not null.
The syntax of usage of the Null Coalescing Operator is like the following snippet:
- <?php
- //Old way
- //New way
- $x = $a ?? $b;
- ?>
As you can see in the sample snippet above, the new way of Null Coalescing in PHP is more likely easier and less coding while both ways will return the same value;
Example
Here is the script of a simple web application that demonstrates both the old and new way of writing a null coalescing syntax using the PHP's built-in isset() method and null coalesce operator.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
- <link rel="stylesheet" href="assets/css/styles.css">
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
- <style>
- </style>
- </head>
- <body>
- <main>
- <nav class="navbar navbar-expand-lg navbar-dark bg-gradient">
- <div class="container">
- <div>
- </div>
- </div>
- </nav>
- <div id="main-wrapper">
- <div class="container px-5 my-3" >
- <script>
- start_loader()
- </script>
- <div class="mx-auto col-lg-8 col-md-10 col-sm-12 col-xs-12">
- <div class="card rounded-0 mb-3 shadow">
- <div class="card-header rounded-0">
- </div>
- <div class="card-body rounded-0">
- <div class="container-fluid">
- <div class="bg-dark text-light" style="height: 35vh; overflow: auto;">
- <?php
- <br>
- <span class="text-info">$page</span> = <span class="text-warning">isset</span>(<span class="text-info">$_GET</span><span class="text-primary">[</span><span class="text-warning">'page'</span><span class="text-primary">]</span>) ? <span class="text-info">$_GET</span><span class="text-primary">[</span><span class="text-warning">'page'</span><span class="text-primary">]</span> : "home";<br>
- <br>
- <br>
- <br>
- <br>
- <span class="text-info">$postID</span> = <span class="text-warning">isset</span>(<span class="text-info">$_GET</span><span class="text-primary">[</span><span class="text-warning">'id'</span><span class="text-primary">]</span>) ? <span class="text-info">$_GET</span><span class="text-primary">[</span><span class="text-warning">'id'</span><span class="text-primary">]</span> : (<span class="text-warning">isset</span>(<span class="text-info">$_GET</span><span class="text-primary">[</span><span class="text-warning">'postID'</span><span class="text-primary">]</span>) ? <span class="text-info">$_GET</span><span class="text-primary">[</span><span class="text-warning">'postID'</span><span class="text-primary">]</span> : "Variable is NULL");<br>
- <br>
- <br>
- <span class="text-info">$postID</span> = <span class="text-info">$_GET</span><span class="text-primary">[</span><span class="text-warning">'id'</span><span class="text-primary">]</span> ?? <span class="text-info">$_GET</span><span class="text-primary">[</span><span class="text-warning">'postID'</span><span class="text-primary">]</span> ?? "Variable is NULL";<br>
- <br>
- ?>
- <br>
- </div>
- <?php
- //Coalescing using PHP isset() method
- $page = isset($_GET['page']) ? $_GET['page'] : "home";
- echo "<label class='text-muted fw-light fst-italic'>Page Variable using isset() method:</label>";
- //Coalescing using PHP ?? operator
- $page = $_GET['page'] ?? "home";
- //Multiple in-line Coalescing using PHP isset method;
- $postID = isset($_GET['id']) ? $_GET['id'] : (isset($_GET['postID']) ? $_GET['postID'] : "Variable is NULL");
- //Multiple in-line Coalescing using PHP ?? Operator;
- $postID = $_GET['id'] ?? $_GET['postID'] ?? "Variable is NULL";
- ?>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- <footer class="bg-gradient bg-light shadow-top py-4 col-auto">
- <div class="">
- <div class="text-center">
- </div>
- <div class="text-center">
- </div>
- </div>
- </footer>
- </main>
- </body>
- </html>
DEMO VIDEO
Save the script above as a PHP File and try to run it on your end to test it if works properly and meets the main objectives of this tutorial.
I also have provided a simple web application source code zip file that I created for this tutorial on this website. It is free to download and can be downloaded by clicking the Download Button located below this article's content.
That's the end of this tutorial! I hope this Null Coalescing in PHP Tutorial will help you with what you are looking for and will be useful for your current and future PHP Projects.
Explore more on this website for more Tutorials and Free Source Codes.
Happy Coding =>
Add new comment
- 246 views