-2

I got an array implode variable, $varString, that is setup to return 3 separate values listed below depending on the condition.

  • 1
  • 2
  • 1,2

If ($varString == 1) 
{
    echo 'APPLE';}
ElseIf ($varString == 2) 
{
echo 'BANANA';}
ElseIf ($varString == 1,2)  //throws an error. 
{
echo 'APPLE and BANANA';}

How do I do this for the case of 1,2?

I tried

ElseIf ($varString == '1,2')  //throws an error. 
{
echo 'APPLE and BANANA';}

ElseIf ($varString == "1,2")  //throws an error. 
{
echo 'APPLE and BANANA';}
user3527285
  • 79
  • 1
  • 8

2 Answers2

1

As 1,2 could only be understood by PHP as a string, you should change your script to this:

If ($varString == '1') 
    {
        echo 'APPLE';
    }
    ElseIf ($varString == '2') 
    {
        echo 'BANANA';
    }
    ElseIf ($varString == '1,2')  //no it doesn't 
    {
        echo 'APPLE and BANANA';
    }

and also, string should always be in ''

Alex Szabo
  • 3,274
  • 2
  • 18
  • 30
  • Usually single quotes should be used. Not always, though. – kevin628 Sep 02 '14 at 14:03
  • @kevin628 I sometimes use double quotes (especially when I'm writing SQL inside PHP). Are there any rules for when should what be used? For what I can remember it was something like that you could include $variables inside ''s but if they were in ""s it would take literally the $variable text, not its value. – Alex Szabo Sep 02 '14 at 14:07
  • 1
    I go by the docs: http://php.net/manual/en/language.types.string.php. Occasionally, for some larger applications, I've needed special escape sequences, and the best way to do it was with double quotes. And a few templating frameworks use variable expansion, which requires double quotes. Beyond cases where one can sanely prove the need for double quotes, then one should stick with single-quotes. – kevin628 Sep 02 '14 at 14:55
0
ElseIf ($varString == '1,2') { echo 'APPLE and BANANA';}
Traian Tatic
  • 681
  • 5
  • 18