php - 数组值未传递给函数
问题描述
代码解释了查找最大值和最小值,但是,我在函数上传递了一个数组但是输出似乎没有出现在浏览器上,它是空白的。我假设逻辑是 100% 好的。另外,我希望有人教我如何在函数中传递数组的语法。因为我无法在 PHP 中传递数组值。一个解决方案会很棒
<?php
$test=array(9,7,3,13,1); // sample array
function maximum($array) // function to find maximum value in an array
{
$slot=$array[0]; // fixed the first value of the array
$length=count($array); // length of the array
for($i=1; $i<$length; $i++)
{
$slot = ($slot<$array[$i]) ? $array[$i] : " "; // comparing the fixed value with other values
}
echo $slot."<br>"; // final max value
}
function minimum($array) // function to find minimum value in an array
{
$slot=$array[0]; // fixed the first value of the array
$length=count($array); // length of the array
for($i=1; $i<$length; $i++)
{
$slot= ($slot>$array[$i]) ? $array[$i] : " "; // comparing the fixed value with other values
}
echo $slot; // final max value
}
echo maximum($test); //passing the $test array to find the max
echo minimum($test); //passing the $test array to find the min
?>
解决方案
要调用函数,只需使用以下格式
maximum($test);
minimum($test);
无需在函数前加上 echo
请把逻辑改成
$slot= ($slot>$array[$i]) ? $array[$i] : $slot;
推荐阅读
- css - 删除材质对话框上的奇怪边框
- c - 如何使这项工作适用于 3 个用户并通过而不是 1 C
- angular - 带有参数的特定路由的用户访问
- python - 通过硒,Python中的xpath检测用户可见元素(仅在视口中)
- c# - 如何让 C# 中的按钮上显示的文本?
- reactjs - 动作类型说明
- mysql-python - 如何在 Python 2.6 中使用 mysql.connector
- swift - 在 SpriteKit swift4 Xcode 中停止一个动作并设置一个定时器来切换玩家
- r - 根据列名和因子将数据从一个数据框收集到另一个数据框
- android - getUserConfirmation 不起作用反应本机