Error in IF condition

Asked

Viewed 59 times

0

I’m doing a program in PHP but I’m having a problem. I want when $user_info is the same as Tiago Goncalves, to show the user’s image. The truth is that all 3 images are shown on the website.

HTML

  • Colleague, it is not good practice here in the community you misread your question. Think it may be useful for other colleagues in the future. About your editing in reply of the colleague, I believe will not be accepted. Your own question already says: "- when $user_info is equal to Tiago Goncalves.". If you don’t want your name displayed here, seek guidance from moderators.

1 answer

4


Note that you are not using the comparator but assigning the value to the variable:

if( $user_info['nome'] = 'Tiago Gonçalves')

The right thing would be:

if( $user_info['nome'] == 'Tiago Gonçalves')

Correcting the code:

if( $user_info['nome'] == 'Tiago Gonçalves'){
   echo "<img src='img/users/Tiago.jpg'>";
}
else if( $user_info['nome'] == 'Alexandre Salgado'){
   echo "<img src='img/users/Alexandre.jpg'>";
}
else if( $user_info['nome'] == 'Ricardo Cardoso'){
   echo "<img src='img/users/Ricardo.jpg'>";
}
  • Really Pedro... if we come from other languages for PHP/Javascript that use two "=" in comparison, sometimes we get confused! I knew right away that this was it!

  • I recently read that debate at the Meta. I recommend reading, is it of great "help" https://pt.meta.stackoverflow.com/questions/4329/como-ser-um-usu%C3%a1rio-mais-dedicado-no-stack-overflow? cb=1 and applies out of SOPT

  • The images are really different ? The content of $user_info mute ?

  • They are 100% different.

  • Strange that the code I edited above is working perfectly here. Check if the value you are comparing matches the value that comes with the variable. The manual tests I did here worked 100%

  • You used my database?

  • I already solved it. I used user_info but instead of $user_info['name'], I used $user_info['id'] == '1'

  • @Tiagogonçalves mark the answer as right. Since Pedro managed to solve his problem. It is important to Sopt and to him that he deserves to be rewarded after analyzing the code, testing and publishing the correct answer. See more here: https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

Show 3 more comments

Browser other questions tagged

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