You must know these 10 questions!

You must know these 10 questions!

1. Question one is about weak types

$str1 ='yabadabadoo';

$str2 ='yaba';

if (strpos($str1,$str2)) { 

echo "\\"". $str1. "\\" contains\\"". $str2. "\\"";

} else {

echo "\\"". $str1. "\\" does not contain\\"". $str2. "\\"";

}

The output of the correct operation:

"yabadabadoo" does not contain "yaba"

strpos returns the position of the string str2 in str1. If it is not found, it returns false. However, in fact, 0 is returned this time and 0 is also regarded as false in the if statement. Therefore, we need to make a type judgment on false. The correct code is as follows :

$str1 ='yabadabadoo';

$str2 ='yaba';

if (strpos($str1,$str2) !== false) { 

echo "\\"". $str1. "\\" contains\\"". $str2. "\\"";

} else {

echo "\\"". $str1. "\\" does not contain\\"". $str2. "\\"";

}

It should be noted that we have used !==. In php and JS, ==! Relative == is more stringent and requires consistent data types.

2. What will be the output below?

$x = 5;

echo $x; 

echo ""; 

echo $x+++$x++; 

echo ""; 

echo $x; 

echo ""; 

echo $x---$x--; 

echo ""; 

echo $x; 

The actual operation result is

11 

$x++ and $x--this problem is actually very easy to encounter, we only need to remember that $x++ uses the most recent value, and then increments.

The precedence of operators, ++ is significantly higher than +, so ++ is executed first and then + is executed. Regarding the priority of operators, sometimes we can really use parentheses to make our program more intuitive. After all, the code is not only used for execution, sometimes the readability of the team may also improve efficiency. Kind of.

3. Reference to variables;

$a = '1';

$b = &$a;

$b = "2$b";

What is the value of $a and $b?

Part of the first time I think of $a='1' $b='21', take a closer look at $b=&$a, where $b is a reference to the variable $a rather than a direct assignment.

4. Is the following true or false

var_dump(0123 == 123); 

var_dump('0123' == 123); 

var_dump('0123' === 123); 

var_dump(0123 == 123);//false, PHP will treat 0123 as octal by default, and the actual conversion to decimal is 83, which is obviously not equal.

var_dump('0123' == 123);//true where php will be very interesting to convert '0123' into a number and remove the previous 0 by default, which is 123==123

var_dump('0123' === 123);//false Obviously, the above question has already said that the number and string types are inconsistent.

5. Is there anything wrong with the following code? What the output will 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 still see the following warning when running

Warning: array_merge(): Argument #2 is not an array 

Warning: array_merge(): Argument #1 is not an array 

The parameters that array_merge needs to pass in are all arrays, if not, null will be returned. You can modify like this

$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); 

6. What should be the output of $x?

$x = true and false;

var_dump($x); 

Some students may think of false for the first time. In fact, the priority of operators is still emphasized here. = will be higher than and, so it is equivalent to the following code

$x = true;

true and false 

The answer is obvious.

7. What should be the value of $x after the following operations?

$x = 3 + "15%" + "$25"

The answer is 18. PHP will automatically convert the type according to the context.

The above code can be understood in this way. If we are performing mathematical operations with strings, PHP will actually convert the array in the string as much as possible. If it is the beginning of a number, it will be converted to a number. For example, "15%" will become 15, if it is not at the beginning of a number, it will become 0; the above operation is similar to the following:

$x = 3 + 15 + 0

8. Run the following code, what is the value of $text? What result will strlen($text) return?

$text ='John';

$text[10] ='Doe';

After the above code is executed, $text = "John D" (There will be 5 consecutive spaces after John) strlen($text) will return 11.

$text[10] = When "Doe" assigns a specific character in a specific position of a string, only D is actually assigned to $text. Although $text only has 5 conceited lengths, but php will fill spaces by default . This is somewhat different from other languages.

9. What will the following output result be?

$v = 1;

$m = 2;

$l = 3;

if( $l> $m> $v){ 

echo "yes";

}else{

echo "no";

}

The actual output is "no", as long as it is carefully analyzed, it is not difficult to get

$l>$m will be converted to 1, then compare with $m at this time.

10. What value will $x become when executing the following code?

$x = NULL;

if ('0xFF' == 255) { 

$x = (int)'0xFF';

}

The actual running result is $x=0 instead of 255.

First of all,'oxFF' == 255, we are good to judge, we will convert the hexadecimal number to the decimal number, 0xff -> 255.

PHP uses is_numeric_string to determine whether the string contains hexadecimal numbers and then convert it.

But $x = (int)'0xFF'; will it also become 255? Obviously not. Convert_to_long is actually used to cast a string, which actually converts the string from left to right, and stops when it encounters a non-digit character. So 0xFF to x stops. So $x=0

Reference: https://cloud.tencent.com/developer/article/1093118 You must know these 10 questions! -Cloud + Community-Tencent Cloud