doulao1966 2014-03-15 16:02
浏览 26
已采纳

Strlen无法正常工作[关闭]

i'm just making experience on some PHP functions to control some form fields.

I just built a real tiny form to see how can work an OOP form, but i'm experiencing a strange behaviour of strlen command.

Here is the code:

class form and its tiny control

class Form {

public $nome = '';

public function __construct() {
    if(isset($_POST['nome'])){
    $this->nome = $_POST['nome'];
    }
}
public function body() {

    return "<form action=$_SERVER[PHP_SELF] method=post>
           <input type=text name=nome value=\"$this->nome\">
           <input type=submit value=INVIA><br>";
    }

  }

class check {

public $nome;

public function __construct() {
    $this->nome = $_POST['nome'];
}

public function controllo() {

    if ( !empty($this->nome) && strlen($this->nome >= 3) ) {
        echo "$this->nome è un nome valido";
        return TRUE;
    } else {
        return FALSE;
    }
}

}

PHP implementation

<?php
    require '/classes/class.form';

    if(!isset($_POST['nome'])) {
     $form=new Form();
    echo $form->body();   
    } else {
        $check = new check();
        if (!$check->controllo()) {
            $form2=new Form();
            echo $form2->body();
            echo "c'è un errore nei controlli";

        } else {
            echo "tutto ok!";
        }

    }

    ?>

As you see, the check should control if the input text lenght is >=3. If false, the form print out again with the content and with an advise that something is wrong, else it says it's correct.

It happens that if I try with "1" and "2", the control does not make it pass, but when i try with "3", it seems that the script does not match the lenght of the input but its type and it gives me a correct match even if the lenght is a single char.

Moreover, if i try a single alphabetical or a number >3 of alphabetical chars, the control is always false. What can be the problem here?

  • 写回答

1条回答 默认 最新

  • dongmu1951 2014-03-15 16:04
    关注

    You have misplaced your closing parenthesis. strlen should be applied to just the variable $this->nome, not to your boolean expression $this->nome >= 3.

    Your original code: if (!empty($this->nome) && strlen($this->nome >= 3)) {

    What you intended: if ( !empty($this->nome) && strlen($this->nome) >= 3 ) {

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配