strlenphp没有给出正确的结果

问题描述

我尝试使用php进行简单的长度检查。发布的值为12/26,代码为:

            echo "length:" . strlen($this->request->post["expiration"]);
    
            if (strlen($this->request->post["expiration"]==5)){
                    echo "correct";
            }else{
                    echo "wrong";
            
            }
        exit;

结果是:

length:5 wrong 

请问我的错误是什么?

解决方法

请注意以下行:if (strlen($this->request->post["expiration"]==5))。 您基本上是想在整个表达式上使用strlen。尝试以下方法:if (strlen($this->request->post["expiration"])==5)

,
/* Please see the code if it works */

<?php
        echo "length:" . strlen($this->request->post["expiration"]);
        $a= strlen($this->request->post["expiration"]);
            if ($a==5))
            {
                    echo "correct";
            }else
            {
                    echo "wrong";
            
            }
        exit;
     ?>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...