Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP比较字符串错误_Php_String_Compare - Fatal编程技术网

PHP比较字符串错误

PHP比较字符串错误,php,string,compare,Php,String,Compare,我是一名PHP初学者,我正在尝试根据参数分离输入。下面是我的代码,但似乎从未设置$arg变量。有人能指出我的错误吗 $sender = $_GET['sender']; $receiver = $_GET['receiver']; $message = $_GET['message']; $temp = explode( '.', $message); $tempcnt = count($temp); echo $temp[$tempcnt - 1

我是一名PHP初学者,我正在尝试根据参数分离输入。下面是我的代码,但似乎从未设置$arg变量。有人能指出我的错误吗

    $sender = $_GET['sender'];
    $receiver = $_GET['receiver'];
    $message = $_GET['message'];
    $temp = explode( '.', $message);
    $tempcnt = count($temp);
    echo $temp[$tempcnt - 1];
    if($tempcnt > 2)
    {
        if($temp[$tempcnt-1] === 'mp4')
            {$arg = 3;}
        elseif($temp[$tempcnt-1]==='jpg')
            {$arg = 2;}
        else
            {$arg = 1;}
    }
    echo "Value of arg is" . $arg;
我甚至尝试过在if中使用==和===以及strcmp,但仍然是同一个问题。

试试这个:


另请参见其他答案,但有一种可能性尚未提及,即==和===以及strcmp都会区分大小写进行比较。所以他们找不到像.MP4这样的扩展

这种情况下的解决方案是使用strcasecmp

然而,对于这样的问题,首先要做的是输出更多的诊断信息,这样您就可以自己看到哪里出了问题。在本例中,echo$tempcnt;在赋值之后,否则echo在外部if{..}块之后什么也不做。
这样您就可以了解程序流程了。

问题是因为我没有意识到我已经比较了args>2。让它>=2,维奥拉完成了!!
感谢@barmar指出这一点

$message的值是多少?只要$message至少有2个值,您的代码就应该可以工作。里面的人物。否则,$tempcnt>2将失败,并且不会进行任何比较。因此,它适用于foo.bar.jpg,但不适用于foo.jpg。
<?php
$temp strrchr("foo.jpg",".");
if($temp==".mp4")
$arg = 3;
elseif($temp==".jpg")
$arg = 2;
else $arg = 1;
?>