I was wondering whether there's a better way of writing this? I am aware that the answer could be no.
$variable = aMethodToGetSomeValue();
$result = ($variable) ? $variable : null/false/0;
Basically, I want to set the result to $variable
if $variable
is not false, else return as defined.
The above code works, the question is just whether there's a better way of writing the shorthand? Since the use case for me is such that the $variable
is a query to the database.
Similar question but not really what I'm asking for I believe..?
Look up shorthand ternary if PHP >= 5.3
Example:
<?php
$e = "asdf";
$e = ($e) ?: "foo";
var_dump($e); // string(4) "asdf"
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.