Find maximum and minimum value in an array by the for loop
<p>I want to find the maximum and minimum value by the for loop, but problem is that when array value is start from a maximum number like (100,30,50,60) then output is the correct first maximum value then, minimum value. </p>

<p>Otherwise output first minimum value, then maximum value. </p>

<p>any idea?</p>

<pre><code>    //Number serial.
    $number = array(10,15,20,100,25,30);

    for ($i=0; $i&lt;count($number); $i++){
        //Find maximum number by max function.
        if ($number[$i] == max($number)){
            //Print maximum number.
            echo " The max number is $number[$i] &lt;br&gt;" ;
        }
        //Find minimum number by min function.
        elseif ($number[$i] == min($number)) {
            //Print minimum  number.
            echo " The min number is $number[$i] &lt;br&gt;";
        }
    }
//Output: 
//The min number is 10
//The max number is 100 
</code></pre>

<p>When array value is start from a maximum number. </p>

<pre><code> //Number serial.
    $number = array(100,10,15,20,25,30);

    for ($i=0; $i&lt;count($number); $i++){
        //Find maximum number by max function.
        if ($number[$i] == max($number)){
            //Print maximum number.
            echo " The max number is $number[$i] &lt;br&gt;" ;
        }
        //Find minimum number by min function.
        elseif ($number[$i] == min($number)) {
            //Print minimum  number.
            echo " The min number is $number[$i] &lt;br&gt;";
        }
    }
//Output:
//The max number is 100
//The min number is 10
</code></pre>