1

I am learning PHP and my echo is echoing wrong data.

 $user_post=4;

 if($user_post=5){ 
    echo "User has 5 posts."; 
 }else{
    echo "Cannot continue! Not enough posts: $user_post"; die(); 
 }

Thank you for help.

hauron
  • 4,348
  • 3
  • 34
  • 52

3 Answers3

7

your if is not correct. You must use ==, not = to check if somethng equals in PHP.

if($user_post==5)
devondre
  • 482
  • 5
  • 13
2

You have to use comparison operator: equal == (same value) or identical === (same value and same type).

You can explore php manual for learning http://php.net/manual/en/language.operators.comparison.php

Mehedee
  • 336
  • 1
  • 8
1
if($user_post=5)

this is assignment operator each time it will assing 5 to $user_post varibale change your operator to

if($user_post == 5)

or

if($user_post ==== 5)

The difference is frist one will check value only and 2nd one will check its type I hope it will help you

Dani
  • 875
  • 6
  • 13