我对PHP
有奇怪的问题。当我将字符串设置为'<'并且如果我用几个字符串创建新字符串那么当转到'<'时它将停止工作并转到脚本的下一行
$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';
}
结果是:
1 & lt;WHERE id c
我的问题是为什么更少的<
是不能得到像字符串。>
和=
操作符是OK的。但是<
就是不被认可。我做错了什么?
删除一行,它将工作(您自己测试一个):-
<?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)(用于工作目的)
这一行:
$this->_where = ' WHERE ' . $column . chr(0x3e) . '?';
你覆盖了你以前的所有更改,所以难怪你不能看到正确的结果
请尝试这个函数,让我知道它是否会给你想要的输出
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';
}