Jump to content

Function min-max problem


quelle

Recommended Posts

Im working on 2 functions which will check lowest and biggest value in array. So this code works fine, but only problem it isnt function

<?php 
$brojevi = array(25, 14, 62);
    $mxm = $brojevi[0];

    for ($i=0; $i < count($brojevi); $i++) {
    if($brojevi[$i]>$mxm) {
    $mxm = $brojevi[$i];
    	}
    }
    echo $mxm;

/////////////////////////////////////////////////////

	$brojevi = array(25, 14, 62);
    $mnm = $brojevi[0];

    for ($i=0; $i < count($brojevi); $i++) {
    if($brojevi[$i]<$mnm) {
    $mnm = $brojevi[$i];
    	}
    }
    echo $mnm;

?>

And now code which doesnt work - the functions part

<?php
////////////////////////////////////////////////////
function max($niz)
{
	$mxm = $niz[0];

	for ($i=0; $i < count($niz); $i++) {
    	if($niz[$i]>$mxm) {
    	$mxm = $niz[$i];
    		}
    	}
    return $mxm;
}
////////////////////////////////////////////////////
function min($niz){
	$mnm = $niz[0];

	for ($i=0; $i < count($niz); $i++) {
    	if($niz[$i]>$mnm) {
    	$mnm = $niz[$i];
    		}
    	}
    return $mnm;
};
////////////////////////////////////////////////////

$jabuke = array(15, 25, 559, 554);
$kruske = array(12, 9, 2, 44);

max($jabuke);
min($kruske);

?>

I need this badly tommorow :D

Link to comment
https://forums.phpfreaks.com/topic/239819-function-min-max-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.