One of the most error-prone 10 php face questions

Source: Internet
Author: User
Many programmers with the number of interviews with the company, as well as the PHP I have summed up the test questions or their own knowledge of PHP better grasp of the situation, a lot of small partners are beginning a little above his business, in the face of some as long as the careful to complete the PHP interview problem, but because above his business and make mistakes, Now many companies will be some of these PHP interview questions, the first is to test your knowledge of the basics, the second is to test the programmer's careful, today we will give you a summary of the total error-prone face test!

1. 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 results for proper operation:

"Yabadabadoo" does not contain "Yaba"

Strpos is the return string str2 the position of the str1, and returns False if it is not found however actually this time returns 0 and in the IF statement 0 is also treated as false, so we need to do type judgment on false, 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 = = More stringent requires consistent data types.

2. What will be the output of the following results?

$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  7  1  5

About $x + + and $x--this problem is actually very easy to meet, and we just need to remember that $x++ uses the nearest value before it increases itself.

Operator priority, + + is significantly higher than +, so first execute + + then execute +. On the precedence of operators, sometimes we can really use parentheses to make our program more intuitive to understand, after all, the code is not only for execution, sometimes the team's readability is also a way to improve efficiency.

3. References to variables;

$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 ', take a closer look at $b =& $a, where $b is a reference to the variable $ A instead of a direct assignment.

4. Whether the following is true or False

Var_dump (0123 = = 123);  Var_dump (' 0123 ' = = 123);  Var_dump (' 0123 ' = = = 123);

Var_dump (0123 = = 123), or//false,php will default to 0123 as 8 binary processing, 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 previous 0 that is 123==123

Var_dump (' 0123 ' = = = = 123);//False It's obvious that the above question has been said that the numbers and string types are inconsistent.

5. What is the problem 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 be able to see the following warning when you run it.

Warning:array_merge (): Argument #2 is not a array  warning:array_merge (): Argument #1 is isn't an array

Array_merge parameters that need to be passed in are arrays, and if not, NULL is returned. 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);

6. What should $x be output?

$x = True and False;var_dump ($x);

Some students may think of false the first time, in fact, this is still the priority of the emphasis operator, = will be higher than the and level, so the equivalent of the following code

$x = True;true and False

The answer is obvious.

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

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

The answer is that 18,php is automatically converted based on the context implementation type

The above code we can understand that if we are in a mathematical operation with the string, the actual PHP will try to convert the array in the string, if it is the beginning of the number of conversion to change the number such as "15%" will become 15, if not the beginning of the number will become 0; The above operation resembles the following:

$x = 3 + 15 + 0

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

$text = ' John '; $text [ten] = ' Doe ';

When the above code executes $text = "John D" (There will be 5 consecutive spaces after John) strlen ($text) returns 11

$text [] = "Doe" gives a string a specific position of a specific character at a time, actually only assigns D to $text. Although $text only starts with 5 ego lengths, PHP fills the blanks by default. It's a little 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 careful analysis is not difficult to draw

$l > $m will be converted to 1, then compare it with $m.

10. What is the value of executing the following code $x?

$x = NULL; if (' 0xFF ' = = 255) {      $x = (int) ' 0xFF ';}

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

First ' oxff ' = = 255 We are good to judge, will be converted to convert 16 binary numbers into 10 binary digits, 0xFF, 255.

PHP uses is_numeric_string to determine whether a string contains 16 binary digits and then converts it.

But $x = (int) ' 0xFF '; will it also become 255? Obviously not, forcing a string to be cast is actually using 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

Summarize:

The article summarizes is some error-prone PHP interview questions, in fact, this is the basic simple thing, is to see careful not careful, so in the process of the interview, must see the topic, listen to the problem, review the problem, in the answer is not too late!

Related recommendations:

PHP core technology problem sharing in an interview question


A summary of the written questions in the PHP interview question


Summary of thinkphp Topics in PHP interview questions


A summary of the written questions in the PHP interview question

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.