Vidyalelo
PHP · Q23

Operators And Expressions

Programming · PHP · question 23

Q23

What will be the output of the following PHP code ?

A.
HelloWorld
B.
Hello+World
C.
0
Answer
D.
Error

Answer: Option C

Solution

Answer: Option C
Solution:
In the given PHP code, the concatenation operator (.) should be used instead of the addition operator (+) to concatenate strings. When using the addition operator with strings, PHP will attempt to convert the strings to numbers for addition. If a string cannot be converted to a number, it will be treated as 0.

So, in this code, "two" are treated as numeric strings, which will be converted to 0 when converted to numbers. Therefore, the output of the expression "two" will be 0.

Option A: HelloWorld is incorrect because the correct output is 0, not the concatenation of the two strings.
Option B: Hello+World is incorrect because the correct output is 0, not the two strings separated by a plus sign.
Option D: Error is incorrect because there is no syntax error in the code, but the output is 0