0

Ok so what I want to happen is when a user is logged out my script should check the url for the code 2 and display to the user they have been logged out. If 1 There password or username was wrong and 3 Unsure how they got there. It pulls the correct number but will only display the message for 1. I don't know whats wrong :/

<?php
$err = $_GET['err'];
echo $err;//Proof the code is getting number
if ($err = "1") {
echo '<center><span class="error-box">'; echo "Whoops! Wrong Username or Password. Please try again.</span></center>";
} 
elseif ($err = '2') {
echo '<center><span class="info-box">'; echo "You Have Been Succesfully Logged Out.</span></center>";
} 
else {
echo '<center><span class="info-box">'; echo "Whoops! Somthing funny happened and were not quite sure what your trying to do. Please try again.</span></center>";
}
?>
1
  • 1
    you need to change all the = because it's an assignment operator there is == comparison operator and it compares only the value without caring about the type like 1 as integer when you use == to "1" as a string it returns ture === it compares the value and the type both like $var1 = 1; and $var2 = "2"; if you used === it will return false, and if you used == it will return true Commented Apr 19, 2012 at 0:05

1 Answer 1

6

Because comparison operator is ==, not =.

So you need to have

if ($err == "1") {
Sign up to request clarification or add additional context in comments.

5 Comments

also in php or c many people recommend use if ("1" == $err) instead if ("1" == $err)
There we go, Thank you so much!
@iMysak: I wouldn't say that many, but indeed, some people recommend doing that (though it is a protection from a very obvious mistake that usually detected very soon)
@Tyler Radlick: a typo-like mistake of using = instead of ==
@TylerRadlick - It works because if( "1" == $var) is valid syntax, but if( "1" = $var) means you'd assign a variable to a constant, which is not valid. It's a way to represent if statements to guarantee fixing these typos. That being said, I still prefer the other way, as IMO it is more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.