If null use other variable in one line in PHP
PhpPhp Problem Overview
Is there in PHP something similar to JavaScript's:
alert(test || 'Hello');
So, when test is undefined or null we'll see Hello, otherwise - we'll see the value of test.
I tried similar syntax in PHP but it doesn't seem to be working right... Also I've got no idea how to google this problem..
thanks
Edit
I should probably add that I wanted to use it inside an array:
$arr = array($one || 'one?', $two || 'two?'); //This is wrong
But indeed, I can use the inline '? :' if statement here as well, thanks.
$arr = array(is_null($one) ? "one?" : $one, is_null($two) ? "two ?" : $two); //OK
Php Solutions
Solution 1 - Php
you can do echo $test ?: 'hello';
This will echo $test
if it is true and 'hello'
otherwise.
Note it will throw a notice or strict error if $test
is not set but...
This shouldn't be a problem since most servers are set to ignore these errors. Most frameworks have code that triggers these errors.
Edit: This is a classic Ternary Operator, but with the middle part left out. Available since PHP 5.3.
echo $test ? $test : 'hello'; // this is the same
echo $test ?: 'hello'; // as this one
This only checks for the truthiness of the first variable and not if it is undefined, in which case it triggers the E_NOTICE
error. For the latter, check the PHP7 answer below (soon hopefully above).
Solution 2 - Php
From PHP 7 onwards you can use something called a coalesce operator which does exactly what you want without the E_NOTICE that ?:
triggers.
To use it you use ??
which will check if the value on the left is set and not null.
$arr = array($one ?? 'one?', $two ?? 'two?');
Solution 3 - Php
See @Yamiko's answer below for a PHP7 solution https://stackoverflow.com/a/29217577/140413
echo (!$test) ? 'hello' : $test;
Or you can be a little more robust and do this
echo isset($test) ? $test : 'hello';
Solution 4 - Php
One-liner. Super readable, works for regular variables, arrays and objects.
// standard variable string
$result = @$var_str ?: "default";
// missing array element
$result = @$var_arr["missing"] ?: "default";
// missing object member
$result = @$var_obj->missing ?: "default";
See it in action: Php Sandbox Demo
Solution 5 - Php
As per the latest version use this for the shorthand
$var = $value ?? "secondvalue";
Solution 6 - Php
I'm very surprised this isn't suggested in the other answers:
echo isset($test) ? $test : 'hello';
From the docs isset($var)
will return false if $var
doesn't exist or is set to null.
The null coalesce operator from PHP 7 onwards, described by @Yamiko, is a syntax shortcut for the above.
In this case:
echo $test ?? 'hello';
Solution 7 - Php
If you want to create an array this way, array_map
provides a more concise way to do this (depending on the number of elements in the array):
function defined_map($value, $default) {
return (!isset($value) || is_null($value)) ? $default : $value;
// or return $value ? $default : $value;
}
$values = array($one, $two);
$defaults = array('one', 'two');
$values = array_map('defined_map', $values, $defaults);
Just make sure you know which elements evaluate to false
so you can apply the right test.
Solution 8 - Php
Since php7.4, you can use the null coalescing assignment, so that you can do
$arr = array($one ??= "one?", $two ??= "two ?");
See the docs here
Solution 9 - Php
There may be a better way, but this is the first thing that came to my mind:
echo (!$test) ? "Hello" : $test;
Solution 10 - Php
Null is false in PHP, therefore you can use ternary:
alert($test ? $test : 'Hello');
Edit:
This also holds for an empty string, since ternary uses the '===' equality rather than '=='
And empty or null string is false whether using the '===' or '==' operator. I really should test my answers first.
Solution 11 - Php
Well, expanding that notation you supplied means you come up with:
if (test) {
alert(test);
} else {
alert('Hello');
}
So it's just a simple if...else construct. In PHP, you can shorten simple if...else constructs as something called a 'ternary expression':
alert($test ? $test : 'Hello');
Obviously there is no equivalent to the JS alert
function in PHP, but the construct is the same.
Solution 12 - Php
alert((test == null || test == undefined)?'hello':test);
Solution 13 - Php
I recently had the very same problem.This is how i solved it:
<?php if (empty($row['test'])) {
echo "Not Provided";}
else {
echo $row['test'];}?></h5></span></span>
</div>
Your value in the database is in variable $test..so if $test row is empty then echo Not Provided