Home > Backend Development > PHP Tutorial > Haunted! php string array intersection

Haunted! php string array intersection

WBOY
Release: 2016-08-04 09:20:08
Original
1095 people have browsed it

<code>$where = '1=1';
$keyword = $_GET['keyword'];
        
if($keyword) {
    $where['title'] = array('like', "%$keyword%");
}

var_dump($where);

竟然打印出来:A=1

到底是怎么样的转换流程?</code>
Copy after login
Copy after login

Reply content:

<code>$where = '1=1';
$keyword = $_GET['keyword'];
        
if($keyword) {
    $where['title'] = array('like', "%$keyword%");
}

var_dump($where);

竟然打印出来:A=1

到底是怎么样的转换流程?</code>
Copy after login
Copy after login

First of all, let me complain (if you don’t complain, you will die!):

  1. $where is a string, what the hell is the $where['title'] you wrote?

  2. 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>
Copy after login

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>
Copy after login

Understanding that $where[0] actually refers to $wherethe 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>
Copy after login

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>
Copy after login

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>
Copy after login

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.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template