- PHP Double Question Mark: The Purpose and Use of ?? Operator
- What Does Double Question Mark in PHP Do?
- PHP Double Question Mark Coding Examples
- – How “??” Operator Works With Variable?
- Output Explanation
- – The Behavior of “??” Operator With Condition
- PHP Question Mark Versus PHP Double Question Mark
- – Coding Script Comparing “?:” With “??
- How to Use “?” and “??” in a Single Line?
- – Coding Example of Using “??” and “?:” Together
- Can You Use “??” Multiple Times?
- – Coding Example of Using “??” Multiple Times
- Can You Use PHP Question Mark Operator Repeatedly?
- – Code Script for Repeatedly Using the Question Mark
- Conclusion
- What does double question mark (??) operator mean in PHP ?
- Example
- Output
- Example
- Output
- Double Question Mark in PHP
- Use Double Question Mark as Null Coalescing Operator in PHP
- Use The Double Question Mark on The Values From a Form in PHP
- Related Article — PHP Operator
PHP Double Question Mark: The Purpose and Use of ?? Operator
The PHP double question mark is referred to as the null coalescing operator that checks if the values of the variables are set or a particular condition is true. It keeps the expression to be checked on the right side and the remaining part on the left side. You will find even more interesting facts about the null coalescing operator and study the question mark operator in this article.
Continue reading to explore this miniature double question mark operator that works wonders while making your code concise.
What Does Double Question Mark in PHP Do?
The double question mark in PHP is the shortest possible alternative to the isset() function used with if-else statements. So, you can do the same work with a one-liner code snippet using the given operator instead of writing four to six lines of code.
Moreover, no warning will be thrown when the value of the variable isn’t set or the given condition isn’t true. The PHP double question mark evaluates the expression on the right side and if it doesn’t return true, it jumps to the left side. You can see that it switches between the expressions similar to the if-else blocks.
Please use the PHP double question mark like this: expression1 ?? expression2
PHP Double Question Mark Coding Examples
Unlike the behavior of the PHP double question mark with variables, you will either get nothing or one while dealing with conditions. If the given condition returns true then the double question mark in PHP will return one. In the other case, the stated operator will produce nothing.
So, the value on the left side of the double question mark isn’t used in any case when the right side is caught by a condition. However, you can’t skip writing the left-side value in the statement.
– How “??” Operator Works With Variable?
For instance, you have created a form in your program. Now, you want to assign the value entered in the input field to a variable. In the other case, you want to assign some other value to the same variable. So, you’ll use the PHP double question mark to complete your task.
Here is the coding representation that shows the above scenario and the usage of the double question mark operator in PHP:
// using the double question mark operator |
Output Explanation
You will get the “The value isn’t set yet!” when the page is loaded for the first time. But once you enter a value in the text field and submit the form, you will see the textbox value on your browser.
– The Behavior of “??” Operator With Condition
For instance, you want to check if a condition is true or false. You don’t want to perform any action if the condition is false. All that you require is to get one if the condition is true.
Therefore, you’ll define the condition and add the PHP double question mark operator in front of it. Next, you’ll specify any value such as a string at the end of the statement as shown below.
// defining a variable $str = “PHP Programming”; // using the double question mark with a condition echo strlen($str) == 15 ?? “The length of the string isn’t 15.”; // output: 1 ?> |
PHP Question Mark Versus PHP Double Question Mark
The difference between the PHP question mark and PHP double question mark is that the PHP question mark is a ternary operator and a nice alternative to the if-else statements. However, the double question mark is a null coalescing operator resembling the if statement while working with conditions.
– Coding Script Comparing “?:” With “??
Suppose you want to produce results based on a condition. In that case, you’ll use the “?:” PHP question mark with a colon making a ternary operator. Along with this, you want to only check if a particular condition is true. In such a scenario, you’ll use the “??” PHP double question mark operator.
Please have a look at the code to compare both of the given operators:
// defining a variable $num = 5; // using the question mark operator echo $num * 5 == 25 ? “The value of num is 5” : “The value of num isn’t 5”; // output: The value of num is 5 // using the double question mark operator echo $num * 5 == 25 ?? “The value of num isn’t 5”; // output: 1 ?> |
How to Use “?” and “??” in a Single Line?
Surprisingly, you can use both of the given operators in a single line of code and let your program control hop from one expression to another until the final result is reached.
– Coding Example of Using “??” and “?:” Together
For example, you have an associative array. Now, you want to check if a particular key is set in the array. Next, you want to perform print a statement to set the value of the array if it doesn’t exist. Although the stated task can be achieved by using only the double question mark as well, here is the code snippet that uses both the operators:
// creating a sample array $myArray = array( “key1” => “value1”, “key2” => null, “key3” => “value3”, “key4” => “value4”, ); // using question mark and double question mark echo $myArray[“key2”] ?? “Value is null” ? “Add the value on Key2” : “Key2 is set already”; // output: Add the value on Key2 ?> |
Can You Use “??” Multiple Times?
You can use the PHP double question mark more than one time in a single line of code. The stated feature of the double question mark operator lets you check if any one of the given variables is set. However, it’s always better to add something that is already defined, as the last option.
It is because if you add all the undefined variables then PHP will throw a warning when even the last variable will also be undefined. So, you should either print a statement to inform that none of the given variables are defined or add another defined variable.
– Coding Example of Using “??” Multiple Times
For example, you have some variable names but you aren’t sure if any of them has been defined already. In that case, you’ll write all the variable names separated by double question marks and add an appropriate message as the last option.
Here is how you can use PHP double question mark multiple times:
// defining a single variable $v5 = “Defined!”; // using double question mark multiple times with one defined variable echo $v1 ?? $v2 ?? $v3 ?? $v5 ?? $v4 ?? “None of the variables is set.”; // output: Defined! echo “ ”; // using double question mark multiple times with no defined variable echo $v1 ?? $v2 ?? $v3 ?? $v ?? $v4 ?? “None of the variables is set.”; // output: None of the variables is set. ?> |
Can You Use PHP Question Mark Operator Repeatedly?
You can use the PHP question mark operator repeatedly in a single line of code with different kinds of values similar to the double question mark. Here, the program control will stop jumping through the values once it reaches the first value that is anything but not zero, false, or null.
– Code Script for Repeatedly Using the Question Mark
Imagine that you have some values in your program. Now, you want your program to return the first-found value that isn’t zero, false, or null. So, you’ll add all the values separated by the “?:” ternary operator. In the end, you’ll notice that the operator will not return any warning or notice even if all the values are nothing except null, zero, and false.
Here is the code snippet to help you create the above test script:
echo false ?: null ?: 0 ?: null; // no output echo null ?: 0 ?: 5; // output: 5 ?> |
Conclusion
Coming towards the end of the post, you understood the purpose of the PHP double question mark operator along with comparing and using it with the question mark operator. Now, it’s time to have a look at some very helpful points from the above discussion to successfully shorten your code scripts while performing the tasks efficiently:
- The PHP double question mark operator is an alternative to the isset() function used with the if-else statements
- The PHP double question mark operator is also an alternative to the if statement when working with conditions
- The PHP question mark operator combined with a colon makes a ternary operator
- The ternary operator is an alternative to the if-else statements
- You can use the PHP double question mark and PHP question mark multiple times in a single line of code
Therefore, whether you are dealing with variables or conditions, switching to the double question mark and ternary operator is a wise decision. It is because the shorter your code, the more the readability, and the more time saved.
What does double question mark (??) operator mean in PHP ?
PHP 7 has added a new operator double question mark (??) operator. In PHP 7, the double question mark(??) operator known as Null Coalescing Operator.
It returns its first operand if it exists and is not NULL; otherwise, it returns its second operand. It evaluates from left to right. Null Coalescing operator also can be used in a chain format.
Let’s take the below example to demonstrate the double question mark (??) operator.
Example
Output
Example
Output
- Related Articles
- What does the Double Star operator mean in Python?
- What is the use of the double question mark “??” in Swift?
- What does operator ~= mean in Lua?
- Double not (!!) operator in PHP
- What does the Star operator mean in Python?
- What does the two question marks together (??) mean in C#?
- What Does a Double-Dash in Shell Commands Mean
- What does [Ss]* mean in regex in PHP?
- What is the meaning of the question mark «?» in Swift?
- What is double address operator(&&) in C++?
- What is the «double tilde» (~~) operator in JavaScript?
- Which number should replace the question mark?
- What is the Kotlin double-bang (!!) operator?
- How to remove question mark from corrplot in R?
- A comma operator question in C/C++ ?
Double Question Mark in PHP
- Use Double Question Mark as Null Coalescing Operator in PHP
- Use The Double Question Mark on The Values From a Form in PHP
The double question mark is called Null Coalescing Operator in PHP. It was introduced in PHP7.
The double question mark returns the value from the operands, which is not Null .
It checks the operands from left to the right and returns the first non-Null value.
The Null Coalescing Operator can be used if there is a need to use a ternary in conjunction; before PHP7, we used PHP built-in function isset() with ?: instead of ?? .
Use Double Question Mark as Null Coalescing Operator in PHP
php $Temp = null; $Demo = $Temp ?? 'Nothing'; echo $Demo."
"; $Temp = "Test Double Question Mark"; $Demo = $Temp ?? 'something'; echo $Demo; ?>
The code above will first print “nothing” because the value of $Demo is null, and then it prints the string Test Double Question Mark because the first operand is not null .
Nothing Test Double Question Mark
Use The Double Question Mark on The Values From a Form in PHP
We can use the Null Coalescing Operator on the form values, so if there is not any value inserted, it can print something else. See example:
html> body> form action="test.php" method="post"> Test Value 1: input type="text" name="test1">br> Test Value 2: input type="text" name="test2">br> input type="submit"> form> body> html>
This HTML code will ask you to enter values, and those values will be printed on test.php given below.
php echo $_POST["test1"] ?? $_POST["test2"] ?? "Please enter a test value"; ?>
The code will print the first non-Null value it gets from the form, and if it doesn’t get any value, it will print the output:
Please enter a test value
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.