Home > Article > Backend Development > Haunted! php string array intersection
<code>$where = '1=1'; $keyword = $_GET['keyword']; if($keyword) { $where['title'] = array('like', "%$keyword%"); } var_dump($where); 竟然打印出来:A=1 到底是怎么样的转换流程?</code>
<code>$where = '1=1'; $keyword = $_GET['keyword']; if($keyword) { $where['title'] = array('like', "%$keyword%"); } var_dump($where); 竟然打印出来:A=1 到底是怎么样的转换流程?</code>
First of all, let me complain (if you don’t complain, you will die!):
$where
is a string, what the hell is the $where['title']
you wrote?
You assign an array
to a string within a string, what the hell is that?
After removing some miscellaneous and useless code from your question, I simplified the question:
<code>$where = '1=1'; $where['title'] = array(); var_dump($where);</code>
Corresponding to the above complaint, let’s take a step-by-step look: $where['title']
expresses the character with the subscript 'title'
in the string $where
, pay attention to the subscript The legal value is [0-string length minus 1], so PHP's effect on illegal subscripts is actually the same as $where[0]
.
The problem is further simplified to:
<code>$where = '1=1'; $where[0] = array(); var_dump($where);</code>
Understanding that $where[0]
actually refers to $where
the first character of the string, then the following is what I want to complain about "You assign an array
to a character in a string String, what the hell is this? ”
Let’s do a test:
<code>var_dump( (string)array() );</code>
What do you guess will be output?
<code>PHP Notice: Array to string conversion in /home/nfer/temp.php on line 8 string(5) "Array"</code>
Then it’s easy to understand here, $where[0] = array();
is to assign the string Array
to the first character of the string $where
.
bingo, the output is string(3) "A=1"
Finally, let me also write a haunted code:
<code>$where = 'A=1'; $keyword = $_GET['keyword']; if($keyword) { $where['title'] = $keyword == 123; } var_dump($where);</code>
What do you think the result will be?
1.$where = 1, this is correct, first of all, this is a string.
2. Then you treat $where as an array and assign $where['title'] = array('like',"xxx"). This is unscientific.