473,372 Members | 845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 1278
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.