2

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?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • Should `elseif` be `else if` or vice versa? – Pubby Feb 11 '13 at 13:40
  • Please read your code more closely. `$title = 'Chicago'` (and others) should almost undoubtedly be `$title == 'Chicago'` (or even better yet, `$title === 'Chicago'`). – Corbin Feb 11 '13 at 13:40
  • use `==` all the time for comparison instead of `=`. – ogzd Feb 11 '13 at 13:40
  • Is it normal that you sometimes write 'elseif' and sometimes 'else if' and also sometimes using '"' or ''' to check a string? ' => is for only words " => checks also for variables – Niklas Feb 11 '13 at 13:41
  • you should check for equality with == but in your case you do it only first two times , also trim the values and DEBUG! – v0d1ch Feb 11 '13 at 13:41
  • 1
    I will recommend to use `switch` : http://php.net/manual/en/control-structures.switch.php (more readable in your case) – l2aelba Feb 11 '13 at 13:43

7 Answers7

9

Careful with = and ==. I think that is the problem.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • See this [post](http://stackoverflow.com/a/13783985/1592648) for a simple method to stop this from happening again even by accident. – kittycat Feb 11 '13 at 13:44
6

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.

Intrepidd
  • 19,772
  • 6
  • 55
  • 63
1
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';
        }

you have to be careful for == because = will not work properly in if condition for comparison every time

use == for string and === if numeric

rohitarora
  • 1,380
  • 2
  • 13
  • 20
  • You should probably explain what is wrong instead of just posting code. – kittycat Feb 11 '13 at 13:42
  • sorry for not explaining i was editing my answer i just entered it by mistake. – rohitarora Feb 11 '13 at 13:45
  • "*`=` will not work in if condition*" actually `=` works perfectly fine in `if()` conditions as it is allowed. Also `==` is not only for string and `===` is not only for numeric, they work for any type of comparison. The latter just does a strict comparison. -1 – kittycat Feb 11 '13 at 13:46
  • ok than try this if(1 = 1) { echo 'hi'; } – rohitarora Feb 11 '13 at 13:51
  • You are assigning a constant to a constant, which you can't do *anywhere*. why don't you try this: `if ($x = 'yes'){echo $x;}` – kittycat Feb 11 '13 at 13:53
  • i written this for the best comparison not for that it is strict or something btw thx for information – rohitarora Feb 11 '13 at 13:56
  • what you writing will go into if whatever the condition is true or not – rohitarora Feb 11 '13 at 13:57
  • 1
    You need to read the PHP manual particularly this [page](http://us3.php.net/manual/en/language.operators.assignment.php) and this [page](http://us3.php.net/manual/en/language.operators.comparison.php) so you can get a better understanding on why your answers explanation is misleading and incorrect. Regardless, I'm not going to argue anymore. – kittycat Feb 11 '13 at 14:00
0

Do not use =. You must use == (PHP operators comparison)

Like below:

else if ( $title == "Chicago" )
{
   echo 'This is Chicago';
}
else if ( $title == "Seattle" )
{
   echo 'This is Seattle';
}
else
{
   echo 'No Match Found';
}
730wavy
  • 944
  • 1
  • 19
  • 57
BattleBit
  • 754
  • 7
  • 9
0

In you code example, 'Chicago' and 'Seattle' are being assigned rather compared. "=" vs "=="

For code like this, I would recommend that you checkout the switch statement.

http://php.net/manual/en/control-structures.switch.php

Schleis
  • 41,516
  • 7
  • 68
  • 87
0

try this:

if ( $title == 'New York' )
{
   echo 'This is New York';
}
elseif ( $title == 'California' )
{
   echo 'This Is California';
}
elseif ($title == "Chicago" )
{
   echo 'This is Chicago';
}
elseif ($title == "Seattle" )
{
   echo 'This is Seattle';
}
else
{
   echo 'No Match Found';
}
Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
user2001117
  • 3,727
  • 1
  • 18
  • 18
0

Replace = with ==

    if ( $title == 'New York' )
    {
      echo 'This is New York';
    }
    else if ( $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';
    }

Its working

Community
  • 1
  • 1
Vinod VT
  • 6,946
  • 11
  • 51
  • 75