If only returns the first result

Asked

Viewed 74 times

-1

Hello, I’m having the problem in the code where my "IF" only returns the first result:

if ($aluno['janimal']="1"){
    $animal = "Réptil";}          
elseif($aluno['janimal']="2"){
    $animal ="Cavalo";}
elseif($aluno['janimal']="3"){
    $animal ="Porco";}
elseif($aluno['janimal']="4"){
    $animal ="Tartaruga";}
elseif($aluno['janimal']="5"){
    $animal ="Roedores(Coelho, Chinchila, hamster)";}
elseif($aluno['janimal']="6"){
    $animal ="Peixes";}
elseif($aluno['janimal']="7"){
    $animal ="Aves";}
elseif($aluno['janimal']="8"){
    $animal ="Gato";}
elseif($aluno['janimal']="9"){
    $animal ="Cão";}
elseif($aluno['janimal']="10"){
    $animal ="Invertebrados";}
elseif($aluno['janimal']="11"){
    $animal ="Anfibios";}
else{
    $animal ="Outros";}

Is there a problem?

I call the variable "$animal" at another time....

  • Use two signal of =. Thus ==. When you use only one, you are stating that if the variable $aluno['janimal'] receive the value of 1, then, $animal is equal to Réptil. With two == you are comparing whether the value of the variable is equal to 1 But with that amount of ifs recommend using the switch...case

  • When I put == it gives the following error: Notice: Undefined index: janimal in C: xampp htdocs addemp informacao.php on line 286

  • This means that the variable $animal does not have the value of janimal. Use the function isset to check if the value exists. if (isset($animal['janimal'])) { /* verifica os outros if's */ }

1 answer

3

Has to be == instead of just =

  • @Inkeliz, I think your editing has completely altered the answer, no matter how useful. This type of editing that adds/removes elements in the post that the author even cited are not recommended.

  • @Articuno, reverti.

Browser other questions tagged

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