Home »
PHP »
PHP find output programs
PHP find output programs (conditional statements) | set 3
Find the output of PHP programs | Conditional Statements | Set 3: Enhance the knowledge of PHP conditional statements by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 17, 2021
Question 1:
<?php
define(NUM2, 2);
$NUM1 = $NUM2 + pow(3, 3);
if ($NUM1 % 2 == 0) echo "EVEN";
else echo "ODD";
?>
Output:
ODD
Explanation:
In the above program, we defined a constant NUM2 initialized with 2.
$NUM1 = $NUM2 + pow(3,3);
In the above expression, we used $NUM2 instead of NUM2, then the value of $NUM2 will be 0. Then,
$NUM1 = 0 + pow(3,3);
$NUM1 = 0 + 27;
$NUM1 = 27;
We know that 27 is not completely divisible by 2. Then the condition gets false and "ODD" will be printed on the webpage.
Question 2:
<?php
$NUM = 0x0A;
if ($NUM % 2 == 0) echo "EVEN";
else echo "ODD";
?>
Output:
EVEN
Explanation:
In the above program, we created a variable $NUM, which is initialized a hexadecimal number 0x0A, the decimal value is 10 corresponding to the 0x0A, that's why "EVEN" will print on the webpage.
Question 3:
<?php
$NUM = 0x0A + power(0x10, 2);
if ($NUM <> 110) echo "www.includehelp.com";
else echo "www.google.com";
?>
Output:
PHP Fatal error: Uncaught Error: Call to undefined function power() in
/home/main.php:2
Stack trace:
#0 {main}
thrown in /home/main.php on line 2
Explanation:
The above program will generate a compilation error because power() is not a built-in function in the PHP. We need to use the pow() function to calculate power.
Question 4:
<?php
$NUM = 0x0A + sqrt(0x10);
$VAL = (2 * intval(pi()) + 1) * 2;
if ($NUM == $VAL) echo "www.includehelp.com" . '<br>';
if ($NUM > $VAL) echo "www.google.com" . '<br>';
else echo "Nothing";
?>
Output:
www.includehelp.com
Nothing
Explanation:
In the above program, we created two variable $NUM assigned with expression. The decimal values of 0x0A and 0x010 are 10 and 16 respectively.
$NUM = 0x0A+ sqrt(0x10);
$NUM = 10 + 4;
$NUM = 14;
Now evaluate below expression:
$VAL = (2*intval(pi())+1)*2;
$VAL = (2*3+1)*2;
$VAL = (6+1)*2;
$VAL = (7)*2;
$VAL = 14;
Here, $NUM is equal to $VAL then "www.includehelp.com" will print, after that $NUM>$VAL will false then else part will execute and "Nothing" will print on the webpage.