The issues listed in the article, although seemingly simple, but each behind covers one or several easy to ignore the basic knowledge points, hope to be able to help your interview and work in peacetime.
Q1
The first question about weak types
$str 1 = ' Yabadabadoo ';
$str 2 = ' Yaba ';
if (Strpos ($str 1, $str 2)) {
echo "/" ". $str 1. "/" Contains/"". $str 2. "/"";
} else {
echo '/' '. $str 1. "/" does not contain/"". $str 2. "/"";
}
Output of the correct operation:
"Yabadabadoo" does not contain "Yaba"
Strpos is the return string str2 the position of the STR1, but returns false when it is not found. However, it actually returns 0 and in the IF statement 0 is also treated as false, so we need to make a type judgment on false, and the correct code is as follows:
$str 1 = ' Yabadabadoo ';
$str 2 = ' Yaba ';
if (Strpos ($str 1, $str 2)!== false) {
echo/"". $str 1. "/" Contains/"". $str 2. "/"";
} else {
echo '/' '. $str 1. "/" does not contain/"". $str 2. "/"";
}
Note that we use the!==, in PHP and JS =! Relative to = = more stringent requirements for data type consistency.
Q2
What will the output of the following result be?
$x = 5;
echo $x;
echo "<br/>";
echo $x +++ $x + +;
echo "<br/>";
echo $x;
echo "<br/>";
echo $x---$x--;
echo "<br/>";
echo $x;
The actual running result is
5
11
7
1
5
About $x + + and $x – the problem is actually very easy to meet, and we just need to remember that $x++ uses the nearest value before it increases itself.
Precedence of operators, + + is significantly higher than +, so execute + + before execution +. On the precedence of operators, sometimes we can really use parentheses to make our program more intuitive understanding, after all, the code is not only for execution, sometimes the team's readability is also a kind of improve efficiency.
Q3
A reference to a variable;
$a = ' 1 ';
$b = & $a;
$b = "2$b";
What's the value of $a and $b, please?
Part of the first time will think of $a = ' 1′ $b = ' 21′, a closer look $b =& $a, where $b is a reference to variable $a rather than directly assigned.
Q4
Whether the following is true or False
Var_dump (0123 = = 123);
Var_dump (' 0123 ' = = 123);
Var_dump (' 0123 ' = = = 123);
Var_dump (0123 = = 123);
False,php will default to 0123 as 8 to deal with, the actual conversion to 10 is 83, obviously this is not equal.
var_dump (' 0123′== 123);
True here PHP will be very interesting to convert ' 0123′ to a number and by default remove the front 0 is 123==123
var_dump (' 0123′=== 123);
False it is obvious that the above question has been said that the number and string types are inconsistent.
Q5
Is there a problem with the following code? What will the output be and how to fix it
$referenceTable = Array ();
$referenceTable [' val1 '] = Array (1, 2);
$referenceTable [' val2 '] = 3;
$referenceTable [' val3 '] = Array (4, 5);
$testArray = Array ();
$testArray = Array_merge ($testArray, $referenceTable [' val1 ']);
Var_dump ($testArray);
$testArray = Array_merge ($testArray, $referenceTable [' val2 ']);
Var_dump ($testArray);
$testArray = Array_merge ($testArray, $referenceTable [' Val3 ']);
Var_dump ($testArray);
The actual output is as follows:
Array (2) {[0]=> int (1) [1]=> int (2)}
null
null
You may also see the following warning when running.
Warning:array_merge (): Argument #2 isn't an array
Warning:array_merge (): Argument #1 isn't an array
Array_merge the parameters to be passed in are arrays, and if they are not, they return null. You can change that.
$testArray = Array_merge ($testArray, (array) $referenceTable [' val1 ']);
Var_dump ($testArray);
$testArray = Array_merge ($testArray, (array) $referenceTable [' val2 ']);
Var_dump ($testArray);
$testArray = Array_merge ($testArray, (array) $referenceTable [' Val3 ']);
Var_dump ($testArray);
Q6
What is $x supposed to output?
$x = True and false;
Var_dump ($x);
Some students may think the first time false, in fact, this is still the emphasis on operator precedence, = will be higher than and, so equivalent to the following code
$x = true;
True and False
The answer is obvious.
Q7
What should the value of $x be after the following operation?
$x = 3 + "15%" + "$"
The answer is 18,php is an automatic conversion of the type based on the context
The above code can be understood, if we are in the mathematical operation with the string, the actual PHP will be as far as possible to convert the array in the string, if the number at the beginning of the conversion to change the number, such as "15%" will become 15, if not the beginning of the number will become 0; The above operation is similar to the following:
$x = 3 + 15 + 0
Q8
Run the following code, what is the value of the $text? Strlen ($text) What results will be returned?
$text = ' John ';
$text [Ten] = ' Doe ';
After the above code executes $text = "John D" (There will be 5 consecutive spaces behind John) strlen ($text) will return 11
$text [Ten] = "Doe" gives a particular character of a string specific characters, the actual will only be assigned D to $text. Although $text only started with 5 conceited lengths, PHP fills in the blanks by default. This is a little different from other languages.
Q9
What will the output below be?
$v = 1;
$m = 2;
$l = 3;
if ($l > $m > $v) {
echo "yes";
} else{
echo "no";
}
The actual output is "No", as long as careful analysis is not difficult to draw
$l > $m will be converted to 1, then compare with $m at this time.
Q10
What is the value of executing the following code $x?
$x = NULL;
if (' 0xFF ' = = 255) {
$x = (int) ' 0xFF ';
}
The actual run result is $x=0 rather than 255.
First ' oxff ' = 255 We are good at judging, will be converted to 16 into the number of numbers into 10 digits, 0xFF-> 255.
PHP uses is_numeric_string to determine whether a string contains 16 digits and then converts it.
But $x = (int) ' 0xFF '; Is it going to be 255? Obviously not, forcing a type conversion of a string is actually a convert_to_long, which actually converts the string from left to right and stops when it encounters a non-numeric character. So 0xFF to X stops. So $x=0
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.