【问题标题】:PHP get largest number from 3 variables [duplicate]PHP从3个变量中获取最大数[重复]
【发布时间】:2012-08-06 05:46:48
【问题描述】:

可能重复:
Return variable with the highest value?

我试图想出一种简单的方法来找出 3 个变量中最大的数。

$1 = 100
$2 = 300
$3 = 200

在这 3 个变量中,我想将一个新变量设置为最高的一个 ($2)

所以:

$highest_number = 300

【问题讨论】:

标签: php numbers highest


【解决方案1】:
$highest_number = max($1, $2, $3);

$values = array($1, $2, $3);
$highest_number = max($values);

更多信息请访问the quickref for max

【讨论】:

  • 谢谢,但我已经尝试过了,但是它返回“数组”而不是数字。 :\
  • 数组版本应该是array_max($values)
  • @Michael 来自 PHP 文档:如果第一个也是唯一的参数是一个数组,则 max() 返回该数组中的最大值。如果提供了至少两个参数,则 max() 返回这些值中的最大值。
  • @DylanCross 这绝对是正确的方法。 $1、$2 或 $3 必须是一个数组。尝试var_dump(max($values)); 以查看数组中的确切内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2018-09-28
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
相关资源
最近更新 更多