ADVERTISEMENT
ADVERTISEMENT

PHP find output programs (Operators) | set 1

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

Question 1:

<?php
    $NUM1 = 10;
    $NUM2 = 20;
    
    $RESULT = ++$NUM1 + 2 * $NUM2 + $NUM1++;
    echo $NUM1 . ' ';
    echo $NUM2 . ' ';
    echo $RESULT . ' ';
?>

Output:

12 20 62

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:

$NUM1 is 12
$NUM2 is 20
$RESULT is 62

Question 2:

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

Output:

0

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.

Question 3:

<?php
    $NUM1 = 10;
    $RESULT = 5;
    define(NUM2, 2);
    
    $RESULT *= $NUM1 + PI() * NUM2;
    echo $RESULT . ' ';
?>

Output:

81.415926535898

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.

Question 4:

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

Output:

8

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.

Question 5:

<?php
    $NUM1 = 16;
    define(NUM2, 2);
    redefine(NUM2, 5);
    
    $RESULT = sqrt($NUM1) * 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 redefine() in /home/main.php:4
Stack trace:
#0 {main}
  thrown in /home/main.php on line 4

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.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.