Find the output of PHP programs | Operators | Set 1: Enhance the knowledge of PHP Operators concepts by solving and finding the output of some PHP programs.
Question 1:
<?php
$NUM1 = 10;
$NUM2 = 20;
$RESULT = ++$NUM1 + 2 * $NUM2 + $NUM1++;
echo $NUM1 . ' ';
echo $NUM2 . ' ';
echo $RESULT . ' ';
?>
Question 2:
<?php
$NUM1 = 10;
define(NUM2, 2);
$RESULT *= $NUM1 + NUM2;
echo $RESULT . ' ';
?>
Question 3:
<?php
$NUM1 = 10;
$RESULT = 5;
define(NUM2, 2);
$RESULT *= $NUM1 + PI() * NUM2;
echo $RESULT . ' ';
?>
Question 4:
<?php
$NUM1 = 16;
define(NUM2, 2);
define(NUM2, 5);
$RESULT = sqrt($NUM1) * NUM2;
echo $RESULT . ' ';
?>
Question 5:
<?php
$NUM1 = 16;
define(NUM2, 2);
redefine(NUM2, 5);
$RESULT = sqrt($NUM1) * NUM2;
echo $RESULT . ' ';
?>
Answer 1:
Output:
Explanation:
In the above program, we defined two variables $NUM1 and $NUM2, which are initialized with 10 and 20 respectively.
Note: The operand with pre-increment operator evaluates before the expression and the operand with post-increment operator evaluates after the expression in PHP.
Let's evaluate the expression,
$RESULT = ++$NUM1+2*$NUM2+$NUM1++;
Now we evaluate the pre-increment operator. Here we used a pre-increment operator with $NUM1 then the value of $NUM1 will be 11.
After removing pre and post increment expression will be:
$RESULT = $NUM1+2*$NUM2+$NUM1; $RESULT = 11+2*20+11; $RESULT = 11+40+11; $RESULT = 62;
Now we evaluate post increment operator. Here, we used post-increment operator with $NUM1 then the value of $NUM1 will be 12.
Then the final value of variables:
Question 2:
Output:
Explanation:
The above program will print 0 on the web page because in the above program we did not assign any value $RESULT, that's why it contains 0 value.
$RESULT *= $NUM1 + NUM2;
The above expression used compound multiplication assignment operator then the expression will be:
$RESULT = $RESULT *($NUM1+NUM2); $RESULT = 0 *($NUM1 +NUM2); $RESULT = 0;
So finally, 0 will be printed on the webpage.
Answer 3:
Output:
Explanation:
The above program, will print 81.415926535898 on the web page because in the above program we defined two-variable $NUM1 and $RESULT initialized with 10 and 5 respectively. We also created a constant NUM2 initialized with 2.
$RESULT *= $NUM1 + PI()*NUM2;
In the above expression, we used inbuilt function PI() that will return 3.1415926535898.
The above expression used compound multiplication assignment operator then the expression will be:
$RESULT = $RESULT * ($NUM1 + PI()*NUM2); $RESULT = 5 *(10+3.1415926535898*2); $RESULT = 5 *(10 +6.2831853071796); $RESULT = 5 *16.2831853071796); $RESULT = 81.415926535898;
So finally, 81.415926535898 will be printed on the webpage.
Answer 4:
Output:
Explanation:
In the above program, we declared a variable $NUM1 initialized with 16 and then we defined a constant NUM2 initialized with 5, and then we tried to define the same constant again, but it will not change the previous value, then the value of NUM will remain same that is 2.
$RESULT = sqrt($NUM1)*NUM2;
In the above expression, we used a built-in function sqrt(), which is used to find the square root of a specified number.
Let's evaluate the expression,
$RESULT = sqrt($NUM1)*NUM2; $RESULT = sqrt(16)*2; $RESULT = 4*2; $RESULT = 8;
So finally, 8 will be printed on the webpage.
Answer 5:
Output:
Explanation:
The above program will generate a compilation error because redefine() is not a built-in function. We cannot change the value of a constant.
need an explanation for this answer? contact us directly to get an explanation for this answer