Home  >  Article  >  Backend Development  >  Several pitfalls encountered in PHP interview questions. wall-facing

Several pitfalls encountered in PHP interview questions. wall-facing

WBOY
WBOYOriginal
2016-08-08 09:21:41935browse

1.Pointer hanging problem

$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,2

Explanation:

Let’s analyze it. After the first loop, $value is a reference to the last element in the array. The second loop begins:

Step 1: Copy $arr[0] to $value (note that at this time $value is $arr[2] Reference), then the array becomes [1,2,1]

Step 2: Copy $arr[1] to $value, then the array becomes [1 ,2,2]

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

2. or below Result output:

$test=null;

if(isset($test)){

echo "true";

}else{

echo "false";

}

?>

Correct answer: false

Explanation: For the isset() function, it will return false when the variable does not exist, and it will also be returned when the variable value is null false.

To determine whether a variable is actually set (to distinguish between unset and set values ​​null), the array_key_exists() function may be better.

3. Can the following results be printed and 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 In , unless you explicitly specify a return reference, for the array PHP is a value return, which is a copy of the array. Therefore, when the above code assigns a value to the returned array, it actually assigns a value to the copied array, not the original array. If you change the code to:

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'];

is fine.

Knowledge points: In PHP, for objects, the default is to return by reference, and arrays and built-in basic types are returned by value by default. This should be distinguished from other languages ​​(many languages ​​pass arrays by reference).

4. What does the server output after running the following code?

$.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 Content-Type for application/x-www-form-urlencoded or multipart/form-data for Http requests. The reason for this is for historical reasons. When PHP was first implemented $_POST, the above two types were the most popular. Therefore, although some types (such as application/json) are very popular now, automatic processing is still not implemented in PHP. Because $_POST is a global variable, changing $_POST will be globally effective. Therefore, for requests whose Content-Type is application/json , we need to manually parse the json data, and then modify the $_POST variable.

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

This explains why WeChat public platform development also uses this method to obtain the data of WeChat server post

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: There is no chardata type in PHP, only the stringtype. Understand this, then perform an increment operation on 'z', and the result will be 'aa'. Regarding string size comparison, those who have learned C should all know that 'aa' is smaller than 'z'. This also explains why there is the above output result.

But in PHP, if you compare two purely numeric strings, you first try to compare them as numbers.

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces several pitfalls encountered in PHP interview questions. Face the wall ing, including the content, I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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