469,307 Members | 2,466 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

Using variables in Two Function?

I have a page that has two functions on it, on load I check for posted
value if exists I set some variables that I want to use in the
functions but don't want to pass back and forth. Do I need to be using
global variables in order to use this variable in each function? Are
global variables only available to the page they are called correct?
Never used them before but I'm not getting the values if I just set a
$var at the top of the page. Thanks

May 22 '06 #1
5 1137
You can either use globals (a) or pass a variable pointer to the
function (b).

a)
$foo = true;
function f($a, $b) {
global $foo;
$foo = false;
}
f(1, 2);

b)
$foo = true;
function f($a, $b, &$global) {
$global = false;
}
f(1, 2, $foo);

Cheers

May 22 '06 #2
Barkster wrote:
I have a page that has two functions on it, on load I check for posted
value if exists I set some variables that I want to use in the
functions but don't want to pass back and forth. Do I need to be using
global variables in order to use this variable in each function? Are
global variables only available to the page they are called correct?
Never used them before but I'm not getting the values if I just set a
$var at the top of the page. Thanks


Global variables are like any other variables - only available in that page.

Rather than define them as global, however, you should pass them as parameters
to the functions where they are required.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 22 '06 #3
This is the simplified way I have it now:

$var = true;
$var2 = 2;

function getval() {
display();
}

function display() {
if($var) echo 'true' //not getting a value for $var?
}

<body>
</php getval(); ?>
</body>

May 22 '06 #4
Barkster wrote:
This is the simplified way I have it now:

$var = true;
$var2 = 2;

function getval() {
display();
}

function display() {
if($var) echo 'true' //not getting a value for $var?
}

<body>
</php getval(); ?>
</body>

As others have said, there are many ways to do this. Either:

function display($var) {
if( $var ) echo 'true';
]

would work, or:

function display() {
global $var;
if( $var ) echo 'true';
}

would also work.

-david-

May 22 '06 #5
Thanks, got it to work.

May 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Steven Scaife | last post: by
16 posts views Thread by Martin Jørgensen | last post: by
18 posts views Thread by Ronald Bruck | last post: by
6 posts views Thread by =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post: by
reply views Thread by Luis Zarrabeitia | last post: by
15 posts views Thread by r0g | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.