Tuesday, June 25, 2019

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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...