Several pitfalls encountered by PHP interview questions. Face wall ing

Source: Internet
Author: User
Several pitfalls encountered in PHP interview questions .... Face wall ing1. pointer suspension problem $ array & nbsp; [1, & nbsp; 2, & nbsp; 3]; echo & nbsp; implode (, & nbsp; $ array), & nbsp; & quot; n & quot; foreach & nbsp; ($ array & nbsp; as & nbsp; & amp; $ value) & nbsp; {several pitfalls encountered by PHP interview questions .... Face wall ing

1. pointer suspension

$ Array = [1, 2, 3];

Echo implode (',', $ array), "\ n ";

Foreach ($ array as & $ value) {}// by reference

Echo implode (',', $ array), "\ n ";

Foreach ($ array as $ value) {}// by value (I. e., copy)

Echo implode (',', $ array), "\ n ";

The correct answer should be:

1, 2, 3

1, 2

Explanation:

Let's analyze it. After the first loop, $ value is the reference of the last element in the array. The second cycle begins:

Step 1: Copy $ arr [0] to $ value (note that $ value is a reference of $ arr [2] At this time). then, the array is changed to [1, 2, 1].

Step 2: Copy $ arr [1] to $ value. then, the array is changed to [1, 2].

Step 3: Copy $ arr [2] to $ value. then, the array is changed to [1, 2].

2. output the following results:

$ Test = null;

If (isset ($ test )){

Echo "true ";

} Else {

Echo "false ";

}

?>

Correct answer: false

Explanation: For the isset () function, false is returned if the variable does not exist, and false is returned if the variable value is null.

The array_key_exists () function may be better at determining whether a variable is actually set (distinguish unset and set to null.

3. Can the following results be printed? why?

Class Config {

Private $ values = [];

Public function getValues (){

Return $ this-> values;

}

}

$ Config = new Config ();

$ Config-> getValues () ['test'] = 'test ';

Echo $ config-> getValues () ['test'];

Correct answer:

No, because in PHP, unless you specify the returned reference, the PHP array is returned as a value, that is, copying an array. Therefore, the code above assigns a value to the returned array, which is to assign a value to the copy array instead of the original array. If you change the code:

Class Config {

Private $ values = [];

// Return a REFERENCE to the actual $ values array

Public function & getValues (){

Return $ this-> values;

}

}

$ Config = new Config ();

$ Config-> getValues () ['test'] = 'test ';

Echo $ config-> getValues () ['test'];

You can.

Knowledge points: for objects in PHP, the return is a reference by default, and the array and built-in basic types are returned by value by default. This should be different from other languages (many languages use references for arrays ).

4. what does the server output after the following code is run?

$. Ajax ({

Url: 'http: // my. site/ndex. php ',

Method: 'post ',

Data: JSON. stringify ({a: 'A', B: 'B '}),

ContentType: 'application/json'

});

Var_dump ($ _ POST );

Answer: array (0 ){}

Explanation: PHP only parses Http requests whose Content-Type is application/x-www-form-urlencoded or multipart/form-data. This is because of historical reasons. When PHP initially implemented $ _ POST, the most popular ones were the above two types. Therefore, although some types (such as application/json) are very popular, PHP still does not implement automatic processing. Because $ _ POST is a global variable, changing $ _ POST will be globally valid. Therefore, for requests whose Content-Type is application/json, we need to manually parse the json data and modify the $ _ POST variable.

$ _ POST = json_decode (file_get_contents ('php: // input'), true );

This explains why the public platform must use this method to obtain the server post data during development.

6. the output result of the following code is:

For ($ c = 'a'; $ c <= 'Z'; $ c ++ ){

Echo $ c. "\ n ";

}

Correct answer: a... z, aa... yz

Explanation: The char data type does not exist in PHP, and only the string type exists. To understand this, perform the incremental operation on 'Z' and the result is 'A '. For strings that are relatively small, those who have learned C should all know that 'A' is smaller than 'z. This explains why the above output results exist.

However, if PHP compares two strings of pure numbers, it first tries to compare them as numbers.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.