Why does the function return an unexpected value?

3

Why is this function returning me "disapproved"?

$nFis = 90

function resFis(){              
    if($nFis >= 60){       
        return "aprovado";
    }
    else{
        return "reprovado";
    }
}
    
asked by anonymous 02.10.2015 / 20:49

2 answers

4

Because $nFis is not defined in the resFis function.

It is probably a global variable, so you should change your method to:

function resFis(){              
    global $nFis;
    if($nFis >= 60){       
        return "aprovado";
    }
    else{
        return "reprovado";
    }
}

Or pass it by parameter:

function resFis($nFis){              
    if($nFis >= 60){       
        return "aprovado";
    }
    else{
        return "reprovado";
    }
}
    
02.10.2015 / 20:51
1

The error happens because the function is testing an empty variable. To solve, do the following.

At the place where you called the resFis () function, do:

resFiz($nFis);

And in your role do

function resFis($nFis){              
if($nFis >= 60){       
    return "aprovado";
}
else{
    return "reprovado";
}
}
    
02.10.2015 / 20:52