PHP find output programs (Superglobals) | set 1

Find the output of PHP programs | Superglobals | Set 1: Enhance the knowledge of PHP Superglobals concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 22, 2021

Question 1:

<?php
    $A = 100;
    $B = 200;
    
    function fun()
    {
        $GLOBAL['C'] = $GLOBAL['A'] + $GLOBAL['B'];
    }
    
    fun();
    echo $C;
?>

Output:

PHP Notice:  Undefined variable: GLOBAL in /home/main.php on line 7
PHP Notice:  Undefined variable: GLOBAL in /home/main.php on line 7
PHP Notice:  Undefined variable: C in /home/main.php on line 11

Explanation:

The above program will generate an error because $GLOBALS array is used to access global variables, but we used $GLOBAL, that's why error will be generated.

Question 2:

<?php
    $A = 100;
    $B = 200;
    
    function fun()
    {
        $GLOBALS['C'] = $GLOBALS['A'] * (int)fmod(pi() * 3, 2) + $GLOBALS['B'] * printf("RESULT : ");
    }
    
    fun();
    echo $C;
?>

Output:

RESULT : 1900

Explanation:

The above program will print 1900 on the webpage. In the above program, we created two variables $A and B initialized with 100 and 200 respectively. Then we defined function fun(), here we accessed global variables using super-global array $GLOBALS.

Let's evaluate the expression:

$GLOBALS['C'] = $GLOBALS['A']*(int)fmod(pi()*3,2)+$GLOBALS['B']*printf("RESULT : ");
$GLOBALS['C'] = 100*(int)fmod(pi()*3,2)+200*9;
$GLOBALS['C'] = 100*1+200*9;
$GLOBALS['C'] = 100+1800;
$GLOBALS['C'] = 1900;

In the above expression, printf() function will print "RESULT : " and return 9, and fmod() function is used to find the remainder of floating-point number and then we typecast the result into an integer. And finally, we print the value of 'C' that will print 1900 on the webpage.

Question 3:

<?php
    $A = 100;
    $B = "300";
    
    fun();
    
    function fun()
    {
        $A = 200;
        $C = $A + count($GLOBALS['A']) + count($GLOBALS['B']);
        echo $C;
    }
?>

Output:

202

Explanation:

In the above program, we created two variables $A and $B initialized with 100 and "300" respectively. Then we defined function fun(), here we defined a local variable $A with initial value 200.

Let's evaluate the expression:

$C = $A+count($GLOBALS['A'])+count($GLOBALS['B']);
$C = 200+1+1;
$C = 202;

The count() function is used to count the values of the array, here global $A and $B both contain single values, then it count() function will return 1 for both. Then the final value 202 will be printed on the webpage.

Question 4:

<?php
    fun();
    
    function fun()
    {
        $A = 200;
        $C = $A + count($GLOBALS);
    
        echo $C;
    }
?>

Output:

208

Explanation:

In the above program, we defined a function fun(), here we declared a local variable $A, and we used count() function to count default super-global variables stored in $GLOBAL, then it will return 8. If we defined global variables explicitly then the count will increase.

Let's evaluate the expression:

$C = $A+count($GLOBALS);
$C = 200+8;
$C = 208;

Then 208 will be printed on the webpage.

Question 5:

<?php
    fun();
    
    function fun()
    {
        $A = 200;
        $C = $A + count($GLOBALS);
    }
    
    echo $C;
?>

Output:

PHP Notice:  Undefined variable: C in /home/main.php on line 10

Explanation:

The above program will generate PHP Notice. Because $C is a local variable of function fun(), and we are accessing it outside the function, then it will generate PHP notice.






Comments and Discussions!

Load comments ↻






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