Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

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:

Tagged : / / /

The difference between my and local

rajeshkumar created the topic: The difference between my and local

There is a subtle difference.

In the example below, $::a refers to $a in the ‘global’ namespace.
‘local’ temporarily changes the value of the variable, but only within the scope it exists in.
$a = 3.14159;
{
local $a = 3;
print “In block, \$a = $a\n”;
print “In block, \$::a = $::a\n”;
}

print “Outside block, \$a = $a\n”;
print “Outside block, \$::a = $::a\n”;

# This outputs In block,

# This outputs
In block, $a = 3
In block, $::a = 3
Outside block, $a = 3.14159
Outside block, $::a = 3.14159

‘my’ has no effect on the global $a, even inside the block.

$a = 3.14159;
{
my $a = 3;
print “In block, \$a = $a\n”;
print “In block, \$::a = $::a\n”;
}
print “Outside block, \$a = $a\n”;
print “Outside block, \$::a = $::a\n”;

# This outputs
In block, $a = 3
In block, $::a = 3.14159
Outside block, $a = 3.14159
Outside block, $::a = 3.14159

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

scmuser replied the topic: Re: The difference between my and local

With more explanation…

Both of them are used to declare local variables.
The variables declared with “my” can live only within the block it was defined and cannot get its visibility inherited functions called within that block, but one defined with “local” can live within the block and have its visibility in the functions called within that block.

Tagged :

How to setup Gitlab in local machine ?

gitlab-setup

Download gitlab from

https://www.gitlab.com/downloads/

Installation, setup, configuration, backup Instructions is given in…

https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
https://github.com/gitlabhq/gitlabhq/blob/6-1-stable/doc/install/installation.md

Tagged : / / / / / / / / / / / / / / / / /