Wednesday, 15 February 2017

Simple PHP If / ElseIf statement not working




I have a really simple if / elseif statement that is not working how I want it to...



        if ( $title == 'New York' )
{
echo 'This is New York';
}
elseif ( $title == 'California' )

{
echo 'This Is California';
}
else if ($title = "Chicago" )
{
echo 'This is Chicago';
}
else if ($title = "Seattle" )
{
echo 'This is Seattle';

}
else
{
echo 'No Match Found';
}


If $title is set as New York or California then the script works, but if it is set as Chicago, Seattle or something else then it just displays 'This Is California'



What am I doing wrong?



Answer



You are using = instead of == for the last cases.



This will always be true since this is an assignation.



Replace those by == and you're good to go.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...