Why does the function return me an unexpected value?

Asked

Viewed 66 times

3

Why this function is returning me "failed"?

$nFis = 90

function resFis(){              
    if($nFis >= 60){       
        return "aprovado";
    }
    else{
        return "reprovado";
    }
}
  • you are not passing to the function resFis() parameter which is $nFis.

2 answers

4


Because $nFis is not defined in the function resFis.

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";
    }
}

1

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

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

resFiz($nFis);

And in your job do

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.