Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不传递给函数的数组值_Php - Fatal编程技术网

Php 不传递给函数的数组值

Php 不传递给函数的数组值,php,Php,代码解释了如何查找最大值和最小值,但是,我在函数上传递了一个数组 但是输出似乎没有出现在浏览器上,它是空白的。我认为逻辑是100%正确的。另外,我希望有人教我如何在函数中传递数组的语法。因为我在PHP中传递数组值时遇到问题。一个解决方案会很好 <?php $test=array(9,7,3,13,1); // sample array function maximum($array) // function to find maximum value in an array { $

代码解释了如何查找最大值和最小值,但是,我在函数上传递了一个数组 但是输出似乎没有出现在浏览器上,它是空白的。我认为逻辑是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

?>  

假设这是一个编写代码的练习,并且您不想使用
min()
max()
,那么您编写的代码有两个问题

第一个是函数实际上不返回任何东西,它们只是
echo
结果

第二个是,当您将当前数字与数组中的新元素进行比较时,如果它更匹配,则交换它,如果它不匹配,则将其设置为
“”

$slot= ($slot>$array[$i]) ? $array[$i] : " ";
因此,
$slot
的结束值通常是一个空格。这应该是

$slot = ($slot<$array[$i]) ? $array[$i] : $slot;

$slot=($slot调用函数只需使用以下格式

     maximum($test); 
     minimum($test); 
无需在函数之前放置echo

请将逻辑更改为


$slot=($slot>$array[$i])?$array[$i]:$slot;

我假设逻辑100%正常
不,您的逻辑是错误的,应该是
$slot=($slotWhat@catcon说。另外,你还应该从函数中返回
$slot
。在另一方面,我没有涉及任何内容。如果我放入$slot,这正确吗?你能解释一下如果我应用这个值会发生什么吗?$slot=($slott关于三元表达式的主要内容(
?:
)最终结果将是两个值中的一个。因此,在这种情况下,将运行else部分,并将
$slot
值设置为
“”
。当你使用
if
时,你可以说你只想在值为真的情况下做一些事情,如果没有
else
则什么都不会发生。如果我想在条件为真的情况下在三元运算符上放置一个值怎么办?语法会是什么样子?你能与三元运算符共享吗?你不能这样做re是一个简短的版本,请看,但只有当值不为false时才有效(它还将第一个值设置为条件本身的结果)。Thanx@Nigel Ren。您的解决方案非常有用:)。我是一个初学者,所以我有很多疑问。我希望我没有问您任何愚蠢的问题:)
     maximum($test); 
     minimum($test);