ADVERTISEMENT
ADVERTISEMENT

PHP program to demonstrate the use of $GLOBALS to access global variables

Here, we are going to demonstrate the use of $GLOBALS to access global variables in PHP.
Submitted by Nidhi, on November 24, 2020

Here, we will create a global variable and access the global variable using $GLOBALS.

Program/Source Code:

The source code to demonstrate the use of $GLOBALS to access global variables is given below. The given program is compiled and executed successfully.

<?php
//PHP program to demonstrate the use of 
//$GLOBALS to access global variables.

$num = 30;

function sumNumber()
{
    $num = 20;
    $res = 0;

    $res = $GLOBALS['num'] + $num;
    return $res;
}

printf("Sum is: %d<br>", sumNumber());
?>

Output:

Sum is: 50

Explanation:

In the above program, we created a global variable $num, and then we created a function sumNumber() to calculate the sum of numbers. We also created a local variable with the name $num inside the sumNumber() function, and then we accessed the global variable using $GLOBALS and then return the sum of local and global variables that will be printed on the webpage.

More PHP Programs »


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.