PHP find output programs (Operators) | set 3

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

Question 1:

<?php
    $X = 7;
    $Y = 12;
    
    $Z = "VALUE: ";
    $Z .= $X * $Y + 2020;
    
    echo $Z;
?>

Output:

VALUE: 2104

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";

Question 2:

<?php
    $X = 7;
    $Y = 12;
    
    $Z = ++$X + pow($Y, 2) * 20;
    
    echo $Z;
?>

Output:

2888

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 ;

Question 3:

<?php
    $X = 7;
    $Y = 3;
    
    $Z = ++$X or $Y;
    
    echo $Z;
?>

Output:

8

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.

Question 4:

<?php
    $X = 7;
    $Y = 3;
    
    (++$X or $Y) ? echo ("Hello") : echo ("Hiii");
?>

Output:

PHP Parse error:  syntax error, unexpected 'echo' (T_ECHO) in 
/home/main.php on line 5

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.

Question 5:

<?php
    $X = 7;
    $Y = 3;
    
    $RESULT = (++$X or $Y) ? ("Hello") : ("Hiii");
    
    echo $RESULT;
?>

Output:

Hello

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.






Comments and Discussions!

Load comments ↻






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