简体   繁体   English

PHP:如果大于x,则x

[英]PHP: if greater than x, then x

I know it may sound a silly question, but I'm trying to make this PHP code as one line: 我知道这听起来可能是一个愚蠢的问题,但是我正在尝试将此PHP代码编写为一行:

$value = result_from_a_function();
if ($value > $maximum)
{
    $value = $maximum;
}

Is it possible to make it one line in PHP? 是否可以在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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM