1

The code explains to find the max and min number, However, I passed an array on the function But the output doesn't seem to appear on the browser, It's blank. I assume the Logic is 100% fine. Also, I would like someone to teach me the syntax of how to pass arrays in a function. Because I have trouble passing array values in PHP. A solution would be great

<?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

?>  
Cupid Chakma
  • 130
  • 12
  • 2
    `I assume the Logic is 100% fine` No, your logic is wrong, it should be `$slot = ($slot<$array[$i]) ? $array[$i] : $slot;` in the maximum function. Similar issue with the minimum – catcon Jun 30 '20 at 06:16
  • 1
    What @catcon said. Plus you should also return `$slot` from the functions. – Ben Hillier Jun 30 '20 at 06:17
  • I am not involving anything, on the else part. So is that correct if I put $slot? – Cupid Chakma Jun 30 '20 at 06:18

2 Answers2

1

Assuming this is an exercise in writing code and you don't want to use min() and max(), there are two problems with the code you have written.

The first is that the functions don't actually return anything, they just echo the result.

The second is that when you compare the current number with the new element in the array, you swap it if it is a better match, but set it to " " if it isn't...

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

so the end value of $slot is usually a single space. This should be

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

So your function (without any major logic changes) should be something like...

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] : $slot; // comparing the fixed value with other values
    }
    
    return $slot; // final max value
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • can you explain me what happens to the value if i apply this? $slot = ($slot<$array[$i]) ? $array[$i] : $slot; because if the condtion is true it will be $slot = $array[$i]; but why put $slot on the else part. If i convert it into if statements. if($slot<$array[$i]) { $slot = $array[$i] } else { $slot = $slot; } – Cupid Chakma Jun 30 '20 at 06:25
  • 1
    The main thing about a ternary expression (`?:`) is that the end result will be one of the two values. So in this case the else part is run and will set the `$slot` value to `" "`. When you use an `if` you can say that you only want to do something if the value is true, if there is no `else` then nothing happens. – Nigel Ren Jun 30 '20 at 06:28
  • what if I want to place on single value on the ternary operator when the condition is true? How will the syntax look like? Can you please share? – Cupid Chakma Jun 30 '20 at 06:34
  • With a ternary - you can't do that. There is a short version, have a look at https://stackoverflow.com/a/52837891/1213708, but that only works if the value isn't false (it also sets the first value to be the result of the condition itself). – Nigel Ren Jun 30 '20 at 06:38
  • Thanx @Nigel Ren. Your solution was very helpful :). I am a beginner so I have lots of doubt man. I hope i did not ask you any stupid questions :) – Cupid Chakma Jun 30 '20 at 06:44
1

To call a function just use below format

     maximum($test); 
     minimum($test); 

No need to put echo before the function

Please change the logic to

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

thirumal mani L
  • 164
  • 1
  • 5