# 10 interesting questions about PHP interview

Source: Internet
Author: User
This article is translated from: Https://www.toptal.com/php/interview-questions, the article listed in the question although seemingly simple, but each behind the one or a few people are easy to ignore the basic points of knowledge, Hope to help you with your interview and your usual work.

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 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.

Q2

What will be the output of the following results?

$x = 5;echo $x;  echo "
"; Echo $x + + + $x + +; echo "
"; echo $x; echo "
"; echo $x---$x--; echo "
"; echo $x;

The actual running result is

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.

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 ', take a closer look at $b =& $a, here $b is a reference to the variable $a instead of directly assigned to the value.

Q4

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.

Q5

What's 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 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);  

Q6

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.

Q7

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

Q8

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 the $text. Although $text only started with 5 ego lengths, PHP will fill in the blanks by default. It's a little different from other languages.

Q9

What will be the output below?

$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.

Q10

What value does the following code $x become?

$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.

  • Related Article

    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.