473,467 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1282
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in...
8
by: Sergio Otoya | last post by:
Hi all, I need to add an input hidden field to an existing form (post). I have tried a couple things like adding the '<INPUT type=hidden name=idSelectedURL value=http://server/documents>' to...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
16
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is...
18
by: Ronald Bruck | last post by:
I have several routines which are used millions of times in my programs, using the Gnu multi-precision software's floating-point reals (see <http://www.swox.se/gmp>). These are of type mpf_t, and...
1
by: baling | last post by:
Hi.... Hi everybody, i have a code that i make in VBA and know I want to use this code in to VB6. But i don't know how to use that code in to VB 6.0 Please correct this code so i can use it in VB...
6
by: trungthanh78 | last post by:
Hello everyone, I'm totally new to the group and I would like to learn from you. Thank you in advance! I need to write a program to track whether a mathematical function has changed during...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
0
by: Luis Zarrabeitia | last post by:
Quoting Joe Strout <joe@strout.net>: I'm sure your credentials are bigger than mine. But he is right. A lot of languages have ditched the "concept" of a static variable on a method (how do you...
15
by: r0g | last post by:
Hi There, I know you can use eval to dynamically generate the name of a function you may want to call. Can it (or some equivalent method) also be used to do the same thing for the variables of a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.