少于<运算符无法识别我是否设置了类似字符串PHP

时间:2016-09-08 08:44:19

标签: php operators

str = 'abcabcabc' result = [] str.scan(/b/) { |match| result << match; break if result.size >= 2 } result #=> ["b", "b"] 我有一个奇怪的问题。当我将字符串设置为'&lt;'时如果我用少量字符串创建新字符串,那么什么时候去'&lt;'停止工作并转到脚本的下一行

PHP
  

结果是:

     

1·; WHERE id c

我的问题是为什么少$a = new SomeObject(); $a->where('id', 13332, "<"); public function where($column, $param, $operator = '=') { echo strlen($operator); if (isset($column) && strlen($operator) > 0) { echo $operator; if ($operator === '>') { $this->_where = ' WHERE ' . $column . '>?'; } else if ($operator == '<') { $this->_where = ' WHERE ' . $column . '<?'; } else if ($operator === '=') { $this->_where = ' WHERE ' . $column . '=?'; } else { $this->_where = ' WHERE ' . $column . $operator . '?'; } $this->_where = ' WHERE ' . $column . chr(0x3c) . '?'; echo '<br/>' . $this->_where . '<br/>'; } else { throw new \Exception('We need to have $column variable like string and $param like Param!', 500); } echo '<br/>c'; } 不能像字符串一样。 <>运算符都可以。但=只是不承认。我做错了什么?

3 个答案:

答案 0 :(得分:2)

删除一行,它将起作用(自己测试一下): -

<?php
error_reporting(E_ALL); //check all type of errors
ini_set('display_errors',1); // display those if any happen

$a = new SomeObject();
$a->where('id', 13332, "<");

public function where($column, $param, $operator = '=') {
    echo strlen($operator);
    if (isset($column) && strlen($operator) > 0) {
        echo $operator;
        if ($operator === '>') {
            $this->_where = ' WHERE ' . $column . '> ?'; // added space
        } else if ($operator == '<') {
            $this->_where = ' WHERE ' . $column . '< ?'; // added space
        } else if ($operator === '=') {
            $this->_where = ' WHERE ' . $column . '= ?'; // added space
        } else {
            $this->_where = ' WHERE ' . $column . $operator . '?';
        }
        //$this->_where = ' WHERE ' . $column . chr(0x3c) . '?';  remove this line
        echo '<br/>' . $this->_where . '<br/>';
    } else {
        throw new \Exception('We need to have $column variable like string and $param like Param!', 500);
    }
    echo '<br/>c';
}

注意: -

不工作的原因: -

您还必须添加空格才能使其正确(由@RiggsFolly评论)(对于显示清酒的浏览器)

你只是在写你的条件。 (评论和@JonStirling的例子: - https://3v4l.org/vCO5Z)(用于工作目的)

答案 1 :(得分:1)

在这一行:

$this->_where = ' WHERE ' . $column . chr(0x3e) . '?';

您覆盖以前的所有更改,因此难怪您无法看到正确的结果

答案 2 :(得分:0)

请尝试使用此功能,让我知道它能为您提供所需的输出

public function where($column, $param, $operator = '=') {
    echo strlen($operator);
    if (isset($column) && strlen($operator) > 0) {
        echo $operator;
        if ($operator === '>') {
            $this->_where = ' WHERE ' . $column . '> ?';
        } else if ($operator == '<') {
            $this->_where = ' WHERE ' . $column . '< ?';
        } else if ($operator === '=') {
            $this->_where = ' WHERE ' . $column . '= ?';
        } else {
            $this->_where = ' WHERE ' . $column . $operator . ' ?';
        }
        if($this->_where != '')
        {
            $this->_where .= ' and ' . $column . chr(0x3e) . ' ?';
        }
        else
        {
          $this->_where = ' WHERE ' . $column . chr(0x3e) . ' ?';
        }
        echo '<br/>' . $this->_where . '<br/>';
    } else {
        throw new \Exception('We need to have $column variable like string and $param like Param!', 500);
    }
    echo '<br/>c';
}