Find the output of PHP programs | Operators | Set 2: Enhance the knowledge of PHP Operators concepts by solving and finding the output of some PHP programs.
Question 1:
<?php
$NUM1 = 16;
define(NUM2, 2);
$RESULT = power($NUM1, 2) * NUM2;
echo $RESULT . ' ';
?>
Question 2:
<?php
$NUM1 = 16;
$NUM2 = "16";
$RESULT = ($NUM1 == $NUM2);
echo $RESULT . '@';
$RESULT = ($NUM1 === 16);
echo $RESULT . '#';
$RESULT = ($NUM1 === $NUM2);
echo $RESULT . '%';
?>
Question 3:
<?php
$NUM1 = 16;
$NUM2 = "16";
$RESULT = ($NUM1 <> $NUM2) ? "HELLO" : "HII";
echo $RESULT;
?>
Question 4:
<?php
$NUM1 = 16;
$NUM2 = "16";
$RESULT = $NUM1 * (16 == $NUM2 && 11 > 2) * 5;
echo $RESULT;
?>
Question 5:
<?php
$NUM1 = 12;
$NUM2 = "13";
$RESULT = $NUM1 * ((16 == $NUM2) ? (11 > 2) ? 6 : 5 : 4);
echo $RESULT;
?>
Answer 1:
Output:
Explanation:
The above program will generate a compilation error because power() is not a built-in function. To calculate the power of a specified number, we need to use pow() function in PHP.
Answer 2:
Output:
Explanation:
In the above program, we defined two variables $NUM1 and $NUM2 initialized with 16 and "16". Here, we used two operators for comparison.
The equal operator will math 16 and "16", but the identical operator will not match. The result of an equal and identical operator will be 1 if operand matched successfully.
$RESULT = ($NUM1==$NUM2); echo $RESULT.'@';
The above statements will print 1@.
$RESULT = ($NUM1==16); echo $RESULT.'@';
The above statements will print 1#.
$RESULT = ($NUM1===$NUM2); echo $RESULT.'%';
And the condition ($NUM1===$NUM2) will not return anything. Then it will print '%' only.
Then the final output "1@1#%" will be printed on the webpage.
Answer 3:
Output:
Explanation:
In the above program, we created two variables $NUM1 and $NUM2 initialized with 16 and "16".
$RESULT= ($NUM1<>$NUM2)?"HELLO":"HII";
In the above expression, we used conditional operator, here we used NOT operator "<>", then condition gets false, then "HII" will be assigned to $RESULT.
Then "HII" will be printed on the webpage.
Answer4:
Output:
Explanation:
The above program will print 80 on the webpage. In the above program, we created two variables $NUM1 and $NUM2 initialized with 16 and "16" respectively.
Let's evaluate the expression:
Then the final value of $RESULT will be printed on the webpage.
Answer5:
Output:
Explanation:
The above program will print 48 on the webpage. In the above program, we created two variables $NUM1 and $NUM2 initialized with 12 and "13" respectively.
Let's evaluate the expression:
$RESULT= $NUM1 * ((16==$NUM2)?(11>2)?6:5:4); $RESULT= 12 * ((16=="13")?(11>2)?6:5:4); $RESULT= 12 * 4; $RESULT= 48;
Then final value of $RESULT will be printed on the webpage.
need an explanation for this answer? contact us directly to get an explanation for this answer