/    Sign up×
Community /Pin to ProfileBookmark

Vairable scope in functions

Hi,

How can I access a variable in a function in another function?
for example :

[CODE]
<?php

$a = 2;

function test1()
{
global $a; // This access the $a

$b = 3; // A variable that is being defined in this function

function test2()
{
global $a; // This access the $a also
global $b; // This DOES NOT ACCESS $b

}

}

?>
[/CODE]

How can I access the $b [b]without[/b] having to send it to the function?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 05.2009 — It is usually better to pass variables as function parameters rather than using global variables, as the latter make for tightly coupled code that is difficult to debug or re-use. If desired, you could pass by reference if you want the function to affect a variable without having to return() it. (Also, defining a function within a function can be a problem if the outer function gets called more than once -- causing a duplicate function name error -- so is generally best avoided.)
[code=php]
<?php
function test1(&$a) // pass by reference
{
$b = 3; // A variable that is being defined in this function
test2($a, $b);
}

function test2(&$a, $b)
{
$a += $b;
}

$foo = 2;
test1($foo);
echo $foo; // 5
?>
[/code]
Copy linkTweet thisAlerts:
@NogDogJul 05.2009 — PS: It should probably be noted at this point that an object-oriented approach may be better suited to organizing the interrelationship between data and functions, depending on what it is you really need to do:
[code=php]
<?php
class Example
{
private $foo;
private $bar;

public function __construct($foo)
{
$this->foo = $foo;
}

public function getFoo()
{
return $this->foo;
}

public function test1()
{
$this->bar = 3;
$this->test2();
}

private function test2()
{
$this->foo += $this->bar;
}
}

$test = new Example(2);
$test->test1();
echo $test->getFoo(); // 5
?>
[/code]
Copy linkTweet thisAlerts:
@asmith20002authorJul 05.2009 — Thanks for your good explanation. ?

So I guess there's no way to make the second function (If inside in the first) read the variables being defined in the first function without passing it.

*Thumbs up on the classes XD
Copy linkTweet thisAlerts:
@NogDogJul 05.2009 — You could do it by declaring the variable as global within [i]both[/i] functions, but this risks collisions with the same variable name outside of the functions, along with all the other baggage that comes with the use of globals within functions (and is in general [i]not[/i] the way I would ever recommend doing this).
[code=php]
<?php
function test1()
{
global $a, $b;
if(!function_exists('test2'))
{
function test2()
{
global $a, $b;
$a += $b;
}
}
$b = 3;
test2();
}
$a = 2;
test1();
echo $a; // 5
?>
[/code]
×

Success!

Help @asmith20002 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...