Haunted! Php string array crossover

Source: Internet
Author: User
Tags array to string
{Code ...}
$ Where = '1 = 1'; $ keyword = $ _ GET ['keyword']; if ($ keyword) {$ where ['title'] = array ('like', "% $ keyword %");} var_dump ($ where); printed out: A = 1 What is the conversion process?

Reply content:
$ Where = '1 = 1'; $ keyword = $ _ GET ['keyword']; if ($ keyword) {$ where ['title'] = array ('like', "% $ keyword %");} var_dump ($ where); printed out: A = 1 What is the conversion process?

First, let me vomit (it will die if you don't speak !) :

  1. $whereIs a string, you write$where['title']What is it?

  2. You putarrayAssign a value to a string. What is this?

After removing some useless code from your problem, let's streamline the problem:

$where = '1=1';$where['title'] = array();var_dump($where);

What corresponds to the above vomit is that we also look at it step by step:
$where['title']Represents a string.$whereAs'title'Note that the valid value of the sub-Mark is [0-the length of the string minus 1]. for php, the invalid subscript is actually$where[0]Is consistent.
The problem is further simplified:

$where = '1=1';$where[0] = array();var_dump($where);

Understand$where[0]Actually, it refers$whereThe first character of the string.You putarrayAssign a value to a string. What is this?"
Here is a test:

var_dump( (string)array() );

What do you guess will be output?

PHP Notice:  Array to string conversion in /home/nfer/temp.php on line 8string(5) "Array"

So here is a good understanding,$where[0] = array();Is to put the stringArrayAssigned$whereThe first character of the string.
Bingo, the output isstring(3) "A=1"

Finally, let me writeHauntedCode:

$where = 'A=1';$keyword = $_GET['keyword'];        if($keyword) {    $where['title'] = $keyword == 123;}var_dump($where);

What do you think the results will output?

1. $ where = 1. this is correct. first, this is a string.
2. then you use $ where as an array and assign $ where ['title'] = array ('like', "xxx") to the array. this is unscientific.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.