Is there in PHP something similar to JavaScript\'s:
alert(test || \'Hello\');
So, when test is undefined or null we\'ll see Hello, otherwis
alert((test == null || test == undefined)?'hello':test);
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
There may be a better way, but this is the first thing that came to my mind:
echo (!$test) ? "Hello" : $test;
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
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';
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.