What is the use of the 'global' keyword in PHP?

 In PHP, the global keyword is used to access a global variable from within a function or method. A global variable is a variable that is defined outside of any function and can be accessed from anywhere in the script.

When a variable is declared as global within a function using the global keyword, it tells PHP that the variable is a global variable, and any changes made to the variable within the function will affect the global variable.

Here's an example:

php
$myGlobalVariable = 10; function myFunction() { global $myGlobalVariable; $myGlobalVariable = 20; } myFunction(); echo $myGlobalVariable; // Output: 20

In the above example, the global keyword is used to access the $myGlobalVariable variable within the myFunction() function. The function then changes the value of the variable to 20, which affects the global variable as well. Finally, the value of the global variable is printed to the screen using the echo statement.

Comments

Popular posts from this blog

How fast do search engines index links on a page?

How well do older people understand technology?

How to generate dynamic html sitemap page for blogspot