数组==双倍相等,

时间:2014-09-02 13:56:58

标签: php arrays operation

我有一个数组implode变量$ varString,它被设置为根据条件返回下面列出的3个单独的值。

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

如何处理1,2?

的情况

我试过

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

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

2 个答案:

答案 0 :(得分:1)

由于1,2只能被PHP理解为字符串,因此您应该将脚本更改为:

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

并且,字符串应始终位于''

答案 1 :(得分:0)

ElseIf ($varString == '1,2') { echo 'APPLE and BANANA';}