PHP find output programs (Operators) | set 2

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.
Submitted by Nidhi, on January 20, 2021

Question 1:

<?php
    $NUM1 = 16;
    define(NUM2, 2);
    
    $RESULT = power($NUM1, 2) * NUM2;
    echo $RESULT . ' ';
?>

Output:

PHP Warning:  Use of undefined constant NUM2 - assumed 'NUM2' 
(this will throw an Error in a future version of PHP) in
 /home/main.php on line 3
PHP Fatal error:  Uncaught Error: Call to undefined function power() in 
/home/main.php:5
Stack trace:
#0 {main}
  thrown in /home/main.php on line 5

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.

Question 2:

<?php
    $NUM1 = 16;
    $NUM2 = "16";
    
    $RESULT = ($NUM1 == $NUM2);
    echo $RESULT . '@';
    
    $RESULT = ($NUM1 === 16);
    echo $RESULT . '#';
    
    $RESULT = ($NUM1 === $NUM2);
    echo $RESULT . '%';
?>

Output:

1@1#%

Explanation:

In the above program, we defined two variables $NUM1 and $NUM2 initialized with 16 and "16". Here, we used two operators for comparison.

  • '==' (equal operator), which is used to match two values.
  • '===' (identical operator), which is used for the exact match of two values.

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.

Question 3:

<?php
    $NUM1 = 16;
    $NUM2 = "16";
    
    $RESULT = ($NUM1 <> $NUM2) ? "HELLO" : "HII";
    echo $RESULT;
?>

Output:

HII

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.

Question 4:

<?php
    $NUM1 = 16;
    $NUM2 = "16";
    
    $RESULT = $NUM1 * (16 == $NUM2 && 11 > 2) * 5;
    
    echo $RESULT;
?>

Output:

80

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.

Question 5:

<?php
    $NUM1 = 12;
    $NUM2 = "13";
    
    $RESULT = $NUM1 * ((16 == $NUM2) ? (11 > 2) ? 6 : 5 : 4);
    echo $RESULT;
?>

Output:

48

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.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.