Haunted! PHP string Array Crossover

Source: Internet
Author: User
Tags array to string
$where = '1=1';$keyword = $_GET['keyword'];        if($keyword) {    $where['title'] = array('like', "%$keyword%");}var_dump($where);竟然打印出来:A=1到底是怎么样的转换流程?

Reply content:

$where = '1=1';$keyword = $_GET['keyword'];        if($keyword) {    $where['title'] = array('like', "%$keyword%");}var_dump($where);竟然打印出来:A=1到底是怎么样的转换流程?

First of all, let me spit on the trough (not to vomit the groove will die!) ):

    1. $whereis a string, what kind of a ghost are you writing $where['title'] ?

    2. You assign an array assignment to a string in a string, what the hell is that?

I'll get rid of some of your problems assorted useless code, and then refine the problem:

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

And above The spit groove corresponds to, we also step by step to see:
$where['title']The expression is the $where character of the subscript in the string, note that the subscript 'title' 's legal value is [0-string length minus 1], then PHP for the illegal subscript, in fact, and $where[0] the role is consistent.
This problem is further simplified to:

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

Knowing that $where[0] actually refers to the $where first character of the string, the following is the " you array assign an assignment to a string in a string, what's the ghost?" "
Let's do a test below:

var_dump( (string)array() );

What do you suppose will output?

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

Well, it's a good idea to $where[0] = array(); assign a string Array to $where the first character of a string.
Bingo, the output isstring(3) "A=1"

Finally, let me also write a haunted code:

$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 right, first this is the string.
2. Then you put the $where as an array and put the $where[' title ' = Array (' Like ', "xxx"), assigning the value of the past, which is not scientifically put.

  • 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.