evelikov92 evelikov92 - 3 months ago 8
PHP Question

less < operator not recognize if I set like string PHP

I have strange problem with

PHP
. When I set the string like '<' and if I make new string with few strings then when is go to '<' is stop to working and go to next row of the script

$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';
}



And the result is:

1< WHERE id c


And my question is why less
<
is cannot get like string. The
>
and
=
operators is OK. But the
<
just is not recognize. What I'm doing wrong?

Answer

Remove one line and it will work (test one below yourself):-

<?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';
}

Note:-

Reason is you are just over-writing your conditions. (@JonStirling comment this link:- https://3v4l.org/vCO5Z) (for working purpose)

You have to add spaces too to make it correct(@RiggsFolly intended too) (For browser showing sake)