ExamVeda
Login
Home
Operators and Expressions in php
?

What will be the output of the following PHP code ?

<?php
$one = "Hello";
$two = "World";
echo "$one"+"$two";
?>

Answer & Solution
Correct Answer: Option C
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, "$one" and "$two" are treated as numeric strings, which will be converted to 0 when converted to numbers. Therefore, the output of the expression "$one" + "$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
Examveda
Question posted by Examveda
Community

Join the Discussion

1 Comment
Aminu Aminu123
Aminu Aminu123 3 years ago
the answer is error
D