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

?>  
3
  • 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 Commented Jun 30, 2020 at 6:16
  • 1
    What @catcon said. Plus you should also return $slot from the functions. Commented Jun 30, 2020 at 6:17
  • I am not involving anything, on the else part. So is that correct if I put $slot? Commented Jun 30, 2020 at 6:18

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

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; }
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.
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?
With a ternary - you can't do that. There is a short version, have a look at 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).
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 :)
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.