Find the output of PHP programs | Operators | Set 3: Enhance the knowledge of PHP Operators concepts by solving and finding the output of some PHP programs.
Question 1:
<?php
$X = 7;
$Y = 12;
$Z = "VALUE: ";
$Z .= $X * $Y + 2020;
echo $Z;
?>
Question 2:
<?php
$X = 7;
$Y = 12;
$Z = ++$X + pow($Y, 2) * 20;
echo $Z;
?>
Question 3:
<?php
$X = 7;
$Y = 3;
$Z = ++$X or $Y;
echo $Z;
?>
Question 4:
<?php
$X = 7;
$Y = 3;
(++$X or $Y) ? echo ("Hello") : echo ("Hiii");
?>
Question 5:
<?php
$X = 7;
$Y = 3;
$RESULT = (++$X or $Y) ? ("Hello") : ("Hiii");
echo $RESULT;
?>
Answer1:-
Output:
Explanation:
The above program will print "VALUE: 2104" on the webpage. In the above program, we created $X, $Y, and $Z initialized with 7, 12, and "VALUE" respectively.
$Z .= $X*$Y+2020;
In the above expression we used compound assignment operator. Now we generalize the expression:
$Z = $Z.($X*$Y+2020); $Z = "VALUE: ".(7*12+2020); $Z = "VALUE: ".(84+2020); $Z = "VALUE: 2104";
Answer 2:
Output:
Explanation:
The above program will print "8" on the webpage. In the above program, we created a variable $X and $Y initialized with 7 and 12.
Let's evaluate the expression:
$Z = ++$X+pow($Y,2)*20 ; $Z = 8+144*20 ; $Z = 8 +2880 ; $Z = 2888 ;
Answer 3:
Output:
Explanation:
The above program will print "8" on the webpage. In the above program, we created two variables $X and $Y initialized with 7 and 3.
Let's evaluate the expression:
$Z = ++$X or $Y; $Z = 8 or 3; $Z = 8;
When we performed "or" operation, if the first condition is true then the second condition will never be checked, that's why 8 is assigned to $Z in the above expression.
Answer 4:
Output:
Explanation:
The above program will generate a syntax error. Because echo() function will not return anything. And we did not use any variable on the left side for assignment.
Answer 5:
Output:
Explanation:
The above program will print "Hello" on the webpage. In the above program, we created variables $X and $Y initialized with 7 and 3.
Let's evaluate the expression:
$RESULT = (++$X or $Y)?("Hello"):("Hiii") ; $RESULT = (1)? ("Hello"):("Hiii") ; $RESULT = "Hello" ;
Then "Hello" will be printed on the webpage.
need an explanation for this answer? contact us directly to get an explanation for this answer