如果 null,则在 PHP 中在一行中使用其他变量

PHP 中有没有类似于 JavaScript 的东西:

alert(test || 'Hello');

因此,当 test 未定义或为 null 时,我们将看到 Hello,否则-我们将看到 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..

谢谢

剪辑

我应该补充一点,我想在数组中使用它:

$arr = array($one || 'one?', $two || 'two?'); //This is wrong

但事实上,我也可以使用 inline’? :’if 语句,谢谢。

$arr = array(is_null($one) ? "one?" : $one, is_null($two) ? "two ?" : $two); //OK
93405 次浏览

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';
alert((test == null || test == undefined)?'hello':test);

There may be a better way, but this is the first thing that came to my mind:

 echo (!$test) ? "Hello" : $test;

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.

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.

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.

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).

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?');

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

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

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';

As per the latest version use this for the shorthand

$var = $value ?? "secondvalue";

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