/    Sign up×
Community /Pin to ProfileBookmark

How do I pass a variable from one function to another?

Say I have code that looks like this:

[code=php]
$a = 5;

function test($a) {
$b = $a+5;
return $b;
}

//How do I get $b to come out of the function?

function test($b) {
$c = $b+5;
return $c;
}

echo(test2($b));[/code]

The resulting answer is 5 when it should be 15, so my variable is not coming out of the first function – how do I fix this? Thanks in advance.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@SheldonJun 20.2007 — [code=php]<?php

function test_one($a) {
$b = $a+5;
return $b;
}

function test_two($b) {
$c = $b+5;
return $c;
}
$a = 5;
$a = test_one($a);
$b = text_two($a);
echo("Answer = {$b}.");
?>[/code]
×

Success!

Help @blade52x 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...