Examveda
Examveda

What will be the output of the following PHP code ?

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

A. HelloWorld

B. Hello+World

C. 0

D. Error

Answer: Option C

Solution(By Examveda Team)

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

This Question Belongs to PHP >> Operators And Expressions In Php

Join The Discussion

Comments ( 1 )

  1. Aminu Aminu123
    Aminu Aminu123 :
    3 months ago

    the answer is error
    D

Related Questions on Operators and Expressions in php