I know it may sound a silly question, but I'm trying to make this PHP code as one line:
$value = result_from_a_function();
if ($value > $maximum)
{
$value = $maximum;
}
Is it possible to make it one line in PHP? Something like
$value = result_from_a_function() [obscure operator] $maximum;
魔术功能是MIN
$value = min($value, $maximum)
是的,请使用三元运算符 :
$value = (result_from_a_function() > $maximum) ? $maximum : $something_else;
Ternary Operators make code shorter in one line thats why i suggest using ternary operators like
$message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest');
or according to your code sample
$value = (result_from_a_function() > $max) ? $max: $false_Sataments;
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.