10 PHP interview questions worth pondering

Source: Internet
Author: User
Although the questions listed in this article seem simple, each of them covers one or several basic knowledge points that are easily overlooked by everyone, hoping to help you with your interview and work in peacetime.

Although the questions listed in this article seem simple, each of them covers one or several basic knowledge points that are easily overlooked by everyone, hoping to help you with your interview and work in peacetime.


Q1

First question about weak types

$str1 = 'yabadabadoo';$str2 = 'yaba';if (strpos($str1,$str2)) {      echo "/"" . $str1 . "/" contains /"" . $str2 . "/"";} else {    echo "/"" . $str1 . "/" does not contain /"" . $str2 . "/"";}

Output result of correct running:

"yabadabadoo" does not contain "yaba"

Strpos returns the position of str2 in str1. if it is not found, false is returned. However, in fact, 0 is returned this time and 0 is also treated as false in the if statement, therefore, we need to determine the type of 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 . "/"";}

Please note that we have used it! =, In php and JS =! The data type must be consistent.

Q2

What is the output result below?

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

The actual running result is

511715

The question about $ x ++ and $ x-is very easy to meet. we only need to remember that $ x ++ uses the nearest value before auto-increment.

The operator priority. ++ is significantly higher than ++. Therefore, ++ is executed first and then +. Sometimes we can use parentheses to give our programs a more intuitive understanding of the priority of operators. after all, the code is not only used for execution, sometimes the readability of the team may also improve efficiency.

Q3

References to variables;

$a = '1';$b = &$a;$b = "2$b";

What are the values of $ a and $ B?

Some of the first time you will think of $ a = '1' $ B = '21', take a closer look at $ B = & $ a. Here $ B is a reference to the variable $ a rather than a direct value assignment.

Q4

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 an 8-in-process by default. the actual conversion to a 10-in-process is 83, which is obviously not equal.

Var_dump ('000000' = 0123); // true here php will be very interesting to convert '000000' into a number and remove the preceding 0, that is, 123 = 0123 by default.

Var_dump ('000000' = 0123); // false it is clear that the numbers and string types are inconsistent as mentioned above.

Q5

Is there any problem with the following code? What is the output 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) }NULLNULL

You may also see the following warning when running

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

The parameters required for array_merge are Arrays. if not, null is returned. You can modify

$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

$ X what should be output?

$x = true and false;var_dump($x);

Some may think of false immediately. In fact, the priority of operators is still emphasized here. = is higher than and, so it is equivalent to the following code.

$x = true;true and false

The answer is obvious.

Q7

What is the value of $ x after the following calculation?

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

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

The above code can be understood as follows. if we perform mathematical operations with strings, php will convert the arrays in strings as much as possible, if it starts with a number, it is converted to a number. for example, "15%" is changed to 15. if it does not start with a number, it is changed to 0. the preceding operation is similar to the following:

$x = 3 + 15 + 0

Q8

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

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

After the above code is executed, $ text = "John D" (there are 5 consecutive spaces after John) strlen ($ text) returns 11

$ Text [10] = "Doe" indicates a specific position of a string. actually, only D is assigned to $ text. although $ text starts to have only five characters at your own length, php will fill in spaces by default. This is somewhat different from other languages.

Q9

What is the output result below?

$v = 1;$m = 2;$l = 3;if( $l > $m > $v){      echo "yes";}else{    echo "no";}

The actual output is "no", which is not hard to be obtained after careful analysis.

$ L> $ m is converted to 1, and then compared with $ m.

Q10

What is the value of $ x when executing the following code?

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

The actual running result is $ x = 0, not 255.

First, 'oxff' = 255. we can determine whether to convert the hexadecimal number to a decimal number, 0xff> 255.

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

But $ x = (int) '0xff '; will it also change to 255? Obviously not. convert_to_long is used to forcibly convert a string. it actually converts the string from left to right and stops when it encounters a non-numeric character. Therefore, 0xFF to x is stopped. So $ x = 0

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.