By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,636 Members | 1,814 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,636 IT Pros & Developers. It's quick & easy.

functions and not so global variables.

P: n/a
Hi,

I want to tidy my code up a bit and in order to do that I need to break some
function down.

so if I have something like

function function_1()
{
$foo = 0;
$bar = 1;

$foobar = $foo + $foo
echo $foobar;
}

and I want to split it in two functions I would maybe get

function function_2()
{
$foobar = $foo + $foo
echo $foobar;
}

function function_1()
{
$foo = 0;
$bar = 1;

function function_2()
}

But of course the problem is that function_2 does not know what $foo and
$bar are equal to because the values are not global.
But how could I make function_2 know about the values defined in
function_1()?

The purpose is just to clean up the code so I do not want to do anything
that might be a security problem.

Many thanks in advance.

Sims

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Sims wrote:
I want to tidy my code up a bit and in order to do that I need to break some
function down and I want to split it in two functions
But of course the problem is that function_2 does not know what $foo and
$bar are equal to because the values are not global.
But how could I make function_2 know about the values defined in
function_1()?


function function_2($foo,$bar) {
$foobar = $foo + $foo
echo $foobar;
}

function function_1() {
$foo = 0;
$bar = 1;

function function_2($foo,$bar)
}

Or if you want to be able to use default values:
function function_2($foo=1,$bar=0) {
$foobar = $foo + $foo
echo $foobar;
}

function function_1() {
$foo = 0;
$bar = 1;

function function_2($foo,$bar)
}

function function_3() {
/* can call the function_2() without arguments
and foo will be 1 and bar would be 0 */
function function_2()
}
//Aho
Jul 17 '05 #2

P: n/a
Sims a écrit:
Hi,

[...]
But of course the problem is that function_2 does not know what $foo and
$bar are equal to because the values are not global.
But how could I make function_2 know about the values defined in
function_1()? Just 'pass' the values as args to function_2 : function
function_2($foo,$bar)

Many thanks in advance.

For nothing

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.