Change language

How do I declare a global variable in PHP?

Syntax:
$variable_name = data;
The following programs illustrate how to declare a global variable.Example 1:
// Demonstrate how declare a global variable 
// Declare a global variable $x = "Geeks" ; $y = "for" ; $z = "Geeks" ;  
// Show value
// String concatenation echo $x . $y . $z ;  
?>
Exit:
GeeksforGeeks
Accessing a global variable inside a function:ways to access a global variable inside a function:
  • Using the global keyword
  • Using the array GLOBALS [var_name]: stores all global variables in an array named $GLOBALS [var_name]. Var_name - this is the name of the variable. This array is also accessible from functions and can be used to directly perform operations on global variables.
Example 2:
// Demonstrate how to declare
// global variable 
// Declaring a global variable $x = "Geeks" ; $y = "for" ; $z = "Geeks" ; $a = 5; $b = 10; function concatenate() { // Using the global keyword global $x , $y , $z ; return $x . $y . $z ; } function add() { // Using GLOBALS [’ var_name ’] $GLOBALS [ ’b’ ] = $GLOBALS [ ’a’ ] + $GLOBALS [ ’ b’ ]; }  
// Print the result echo concatenate(); echo "" ; add(); echo $b ; ?>
Exit:
GeeksforGeeks 15 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method