php将浮点数与输入字段值进行比较

发布于 2024-11-07 19:18:18 字数 2079 浏览 0 评论 0原文

我有一个用户可以输入浮点值的表单。我将这些值发布到 PHP 脚本中,然后比较用户输入的数字是否在某些值之间。如果我发布一个整数,无论​​数字是否超出边界,比较都会返回 true。如果我输入浮点数,无论该数字是否在边界内,比较都会失败。我不傻,我已经在 C++ 中完成了浮点比较,并且我知道如何执行 if( float1 >= float2) return false...

这是我的代码:

//loading the helper
$val = Loader::helper('synerciel_form','synerciel_client');
//adding the fields to the inputs array for validation
$val->between('isolation_des_murs_peripheriques', 2.8, null, t($between.'Isolation des murs 
pèriphèriques'), true, false);

//helper class

class SynercielFormHelper extends ValidationFormHelper {
    const VALID_BETWEEN = 7;
    const VALID_FLOAT = 7;
    private $min;
    private $max;
    private $includeLow;
    private $includeHigh;

 public function between($field, $min, $max, $errorMsg, $includeLow = true, $includeHigh = true) {
        $const = SynercielFormHelper::VALID_BETWEEN;
        $this->min = $min;
        $this->max = $max;
        $this->includeLow = $includeLow;
        $this->includeHigh = $includeHigh;

        $this->addRequired($field, $errorMsg, $const);
    }
   ...
   public function test() {

    $between = new ValidationNumbersBetweenHelper();
    if (!$between->between($this->data[$field], $this->min, $this->max, $this->includeLow, $this->includeHigh)) {
                        $this->fieldsInvalid[] = $f;
}

}

我的验证方法(我相信这里是棘手的部分)

class ValidationNumbersBetweenHelper {

    public function between($data, $min = null, $max = null, $includeLow = true, $includeHigh = true) {

        if ($min && $includeLow) {
            if (!($data >= $min))
                return false;
        } else if ($min) {
            if (!($data > $min))
                return false;
        }
        if ($max && $includeHigh) {
            if (!($data <= $max))
                return false;
        } else if ($max) {
            if (!($data < $max))
                return false;
        }
        return true;
    }

}

I have a form in which users can enter floating point values. I post these values to a php script and i compare if the numbers the users entered are between some values. If i post an integer the comparison returns true no matter if the number exceeded the boundaries. If i entere a floating point number the comparison fails no matter if the number is within the boundaries. I am not stupid, i've done floating point comparisons in c++ and i know how to do an if( float1 >= float2) return false...

here is my code:

//loading the helper
$val = Loader::helper('synerciel_form','synerciel_client');
//adding the fields to the inputs array for validation
$val->between('isolation_des_murs_peripheriques', 2.8, null, t($between.'Isolation des murs 
pèriphèriques'), true, false);

//helper class

class SynercielFormHelper extends ValidationFormHelper {
    const VALID_BETWEEN = 7;
    const VALID_FLOAT = 7;
    private $min;
    private $max;
    private $includeLow;
    private $includeHigh;

 public function between($field, $min, $max, $errorMsg, $includeLow = true, $includeHigh = true) {
        $const = SynercielFormHelper::VALID_BETWEEN;
        $this->min = $min;
        $this->max = $max;
        $this->includeLow = $includeLow;
        $this->includeHigh = $includeHigh;

        $this->addRequired($field, $errorMsg, $const);
    }
   ...
   public function test() {

    $between = new ValidationNumbersBetweenHelper();
    if (!$between->between($this->data[$field], $this->min, $this->max, $this->includeLow, $this->includeHigh)) {
                        $this->fieldsInvalid[] = $f;
}

}

My validation method (i believe here is the tricky part)

class ValidationNumbersBetweenHelper {

    public function between($data, $min = null, $max = null, $includeLow = true, $includeHigh = true) {

        if ($min && $includeLow) {
            if (!($data >= $min))
                return false;
        } else if ($min) {
            if (!($data > $min))
                return false;
        }
        if ($max && $includeHigh) {
            if (!($data <= $max))
                return false;
        } else if ($max) {
            if (!($data < $max))
                return false;
        }
        return true;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2024-11-14 19:18:18

检查警告消息 http://php.net/manual/en/language.operators .comparison.php

您可以使用BC数学函数http://php.net/manual/en/function.bccomp.php

$status = bccomp($left, $right);
if ($status == 0) {
    echo 'equal';
} else if ($status > 0) {
    echo 'left bigger than right';
} else {
   echo 'right bigger than left';
}

希望这有帮助

Check the warning message http://php.net/manual/en/language.operators.comparison.php

You can use BC Math Functions http://php.net/manual/en/function.bccomp.php

$status = bccomp($left, $right);
if ($status == 0) {
    echo 'equal';
} else if ($status > 0) {
    echo 'left bigger than right';
} else {
   echo 'right bigger than left';
}

Hope this helps

游魂 2024-11-14 19:18:18

尝试隔离有问题的代码。将验证函数放入独立的 PHP 文件中并在那里进行测试。尝试检查 $max$min 是否为 !== null,因为 0 也是 false.您可以反转逻辑并删除所有 !。 (例如将 >= 更改为 <),因此您可以使用“小于”来代替“不大于或等于”

Try isolating the troublesome code. Put your validation function into a stand-alone PHP file and test it there. Try checking $max and $min for !== null as 0 is also false. You could reverse the logic and remove all the !s. (e.g. change >= to <) so instead of "not greater than or equal to" you can have "less than"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文