PHP Global and Local Variable

In this tutorial we will learn about declaring global and local variable in PHP.

Initially we will see what is local variable and global variable with examples.

Here if we see the variable is declared inside the function “local” so it will work inside the local function only until we return the value inside the function.

In this we can see that the variable “a” is declared outside the function local, so it won’t work inside the function until we call it.

If we see the output for this we will see an error since “a” is a local variable and it will not work beyond the function “test”.

Now in this code if we see, variable “a” is declared globally and we will get an error if we call it inside any function.

To execute the global variable in the function we need to use predefined variable “global” inside the function.

We will see now that it will return value inside the function as well as outside the function.

References:

Joydeep M
Latest posts by Joydeep M (see all)
Tagged : / / /
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x