Search code examples
phparraysoperation

Array == double equal,


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';}

Solution

  • 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 ''