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:
The
1,2,2
Explain:
Let's analyze that. After the first loop,$value is a reference to the last element in the array. The second loop begins:
First step: Copy $arr [0] to $value(Note that $value is $arr [2] ), when the array becomes [1,2,1]
Step two: Copy $arr [1] to $value, when the array becomes [1,2,2]
Step three: Copy $arr [2] to $value, when the array becomes [1,2,2]
2. The following results are output:
$test =null;
if (Isset ($test)) {
echo "true";
}else{
echo "false";
}
?>
Correct answer:false
Explanation: When a variable does not exist, it returns falsefor the isset () function, and returns When the value of the variable is null False.
Thearray_key_exists () function may be better to determine whether a variable is actually set (the distinction is not set and the value is set to null) .
3. Can the following results be printed out, 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 , unless you display the specified return reference, otherwise for the array PHP is the value returned, which is the copy of the array. So the above code assigns a value to the returned array, which is actually the assignment of the copy array, not the original array assignment. 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 '];
You can do it.
Knowledge points: InPHP for objects, the default is a reference return, and the array and built-in base types are returned by value by default. This is to be distinguished from other languages (many languages are reference to 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 resolves content-type to application/x-www-form-urlencoded or Http request for multipart/form-data . This is because of historical reasons,whenPHP First implemented $_post , the most popular is the above two types. So while some types (such as Application/json) are popular today, There is still no automatic processing in PHP. Because $_post is a global variable, changing $_post is globally valid. So for the content-type application/json request, we need to parse the json manually. data, and then modify the $_post variable.
$_post = Json_decode (file_get_contents (' Php://input '), true);
This explains why the public platform is developed in this way to obtain server post data .
6. The result of the following code output 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 , only the string type. Understand this, then the ' z ' is incremented, the result is ' AA '. For the string comparison size, learn C should know that' AA ' is less than ' z ' . This also explains why the above output is the result.
But if you compare a string of two pure numbers in PHP, you first try to compare it as a number.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the PHP surface of several holes encountered. Wall ing, including aspects of the content, want to be interested in PHP tutorial friends helpful.