5 PHP Interview Questions Series – 2

Question 1: Explain the output of the below code:

Surprisingly to many, the above code will output bool(true) seeming to imply that the and operator is behaving instead as an or.

The issue here is that the =  operator takes precedence over the and  operator in order of operations, so the statement $x = true and false  ends up being functionally equivalent to:

This is, incidentally, a great example of why using parentheses to clearly specify your intent is generally a good practice, in any language. For example, if the above statement $x = true and false  were replaced with $x = (true and false) , then $x  would be set to false as expected.

 

Question 2: What will $x  be equal to after the statement $x = 3 + "15%" + "$25" ?

The correct answer is 18.

We know that PHP supports automatic type conversion based on the context in which a variable or value is being used.

If you perform an arithmetic operation on an expression that contains a string, that string will be interpreted as the appropriate numeric type for the purposes of evaluating the expression. So, if the string begins with one or more numeric characters, the remainder of the string (if any) will be ignored and the numeric value is interpreted as the appropriate numeric type. On the other hand, if the string begins with a non-numeric character, then it will evaluate to zero.

With that understanding, we can see that "15%"  evaluates to the numeric value 15 and "$25"  evaluates to the numeric value zero, which explains why the result of the statement $x = 3 + "15%" + "$25"  is 18 (i.e., 3 + 15 + 0).

 

Question 3: Explain about the value of $text and what will strlen($text) return?

After the above code is executed, the value of $text will be the string “ John      D ” (i.e., “John”, followed by 5 spaces, followed by “D”) and strlen($text)  will return 11.

There are two things going on here:

First of all, since $text  is a string, setting a single element of $text  simply sets that single character to the value specified. The statement $text[10] = 'Doe'  therefore sets that single character to ‘D’ (i.e., the first character in the string “Doe”, since an element of a string can only be a single character).

Secondly, $text[10] = 'Doe'  says to set the 11th character of the string (remember that indices are zero-based) to ‘D’. Prior to that statement, though, the length of the string $text (“John “) was only 5. Whereas compilers or interpreters in other languages might barf (with something akin to an out-of-bounds-index error) when you then attempt to set the 11th character of a 5 character string, PHP instead is very “accommodating” and instead allows this and sets all intermediate characters to blanks.

 

Question 4: What do you know about PEAR in php?

PEAR (PHP Extension and Application Repository) is a framework and repository for reusable PHP components. PEAR is a code repository containing all kinds of php code snippets and libraries.

PEAR also offers a command-line interface that can be used to automatically install packages.

 

Question 5: Differences between print and echo in PHP?

echo  and print  are largely the same in PHP. Both are used to output data to the screen.

The only differences are as follows:

  • echo  does not return a value whereas print  does return a value of 1 (this enables print to be used in expressions).
  • echo  can accept multiple parameters (although such usage is rare) while print  can only take a single argument.

 

Share with:

Categories: PHP