472,985 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

global variable in function

Hello, I'm a newbie using PHP. Having some trouble using a variable
within a function.

I use some mysql code to assign a value to $myvariable.
I verify that it is getting the value with an echo.

Inside a function (in the same php file), I declare :
global $myvariable ;
However, $myvariable has lost it's value.

Is there a php setting I need to change to get this to work?
Any other suggestions?
TIA

Jul 17 '05 #1
7 1738
On 14 May 2005 08:50:03 -0700, "Japhy" <ja************@yahoo.com> wrote:
Hello, I'm a newbie using PHP. Having some trouble using a variable
within a function.

I use some mysql code to assign a value to $myvariable.
I verify that it is getting the value with an echo.

Inside a function (in the same php file), I declare :
global $myvariable ;
However, $myvariable has lost it's value.

Is there a php setting I need to change to get this to work?


No, there aren't any that affect normal global variables.

Post a short example demonstrating your problem. Cut it down as far as you
can, so it's still runnable but shows the issue. (Often this process helps you
find the problem yourself).

Are you doing something like:

<?php
$myvariable = 'x';
echo $myvariable;

function f()
{
global $myvariable;
echo $myvariable;
}
?>

Does this not give the expected result on your system?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
I noticed that Message-ID: <hf********************************@4ax.com>
from Andy Hassall contained the following:
<?php
$myvariable = 'x';
echo $myvariable;

function f()
{
global $myvariable;
echo $myvariable;
}
?>


and of course you'll have to call the function.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3
On Sat, 14 May 2005 17:48:29 +0100, Geoff Berrow <bl******@ckdog.co.uk> wrote:
I noticed that Message-ID: <hf********************************@4ax.com>
from Andy Hassall contained the following:
<?php
$myvariable = 'x';
echo $myvariable;

function f()
{
global $myvariable;
echo $myvariable;
}
?>


and of course you'll have to call the function.


Hm, yes, that's a good idea ;-)

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4
Yes, very similar to your example.

<?PHP
session_start();
$result = mysql_query("SELECT SUM(woh_con_pric) as cp, SUM(woh_billed)
as bl, SUM(woh_cost) as co,
SUM(woh_cost_com) as cc from wohdata WHERE CONT_NUM = $my_contnum AND
WOH_ASOFDATE = $my_wohdate") ;

$my_cp_total = mysql_result($result, 0, cp);
$my_btd_total = mysql_result($result, 0, bl);
$my_ctd_total = mysql_result($result, 0, co);
$my_ctc_total = mysql_result($result, 0, cc);
mysql_free_result($result);
echo "$my_cp_total $my_btd_total $my_ctd_total $my_ctc_total ";
$oRSWOHDATA->Close();
unset($oRSWOHDATA);

Echos the correct figures fine.

Here is the function:
function buildColumnLabels() {
global $my_cp_total;
echo "$my_cp_total is the total";
}

When I pull up the .php file, display is
is the total (number is not displayed)

When I whittle it down to the basic code, it works OK. I am using some
templates to build this app, with some simple hand coding. The
templates generate alot of messy code that I'll have to pick through I
guess. I was hoping it was a 'quick fix'.
Thanks for the suggesions.
J

Andy Hassall wrote:
On 14 May 2005 08:50:03 -0700, "Japhy" <ja************@yahoo.com> wrote:
Hello, I'm a newbie using PHP. Having some trouble using a variable
within a function.

I use some mysql code to assign a value to $myvariable.
I verify that it is getting the value with an echo.

Inside a function (in the same php file), I declare :
global $myvariable ;
However, $myvariable has lost it's value.

Is there a php setting I need to change to get this to work?
No, there aren't any that affect normal global variables.

Post a short example demonstrating your problem. Cut it down as far

as you can, so it's still runnable but shows the issue. (Often this process helps you find the problem yourself).

Are you doing something like:

<?php
$myvariable = 'x';
echo $myvariable;

function f()
{
global $myvariable;
echo $myvariable;
}
?>

Does this not give the expected result on your system?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis

tool

Jul 17 '05 #5
Japhy wrote:
Yes, very similar to your example.

<?PHP
session_start();
$result = mysql_query("SELECT SUM(woh_con_pric) as cp, SUM(woh_billed)
as bl, SUM(woh_cost) as co,
SUM(woh_cost_com) as cc from wohdata WHERE CONT_NUM = $my_contnum AND
WOH_ASOFDATE = $my_wohdate") ;

$my_cp_total = mysql_result($result, 0, cp);
$my_btd_total = mysql_result($result, 0, bl);
$my_ctd_total = mysql_result($result, 0, co);
$my_ctc_total = mysql_result($result, 0, cc);
mysql_free_result($result);
echo "$my_cp_total $my_btd_total $my_ctd_total $my_ctc_total ";
$oRSWOHDATA->Close();
unset($oRSWOHDATA);

Echos the correct figures fine.

Here is the function:
function buildColumnLabels() {
global $my_cp_total;
echo "$my_cp_total is the total";
}

When I pull up the .php file, display is
is the total (number is not displayed)

When I whittle it down to the basic code, it works OK. I am using some
templates to build this app, with some simple hand coding. The
templates generate alot of messy code that I'll have to pick through I
guess. I was hoping it was a 'quick fix'.
Thanks for the suggesions.
J

Where are you calling the function? I don't see it in your example.

Also - please don't top-post. The standard in this newsgroup is
bottom-posting.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #6
Thanks to all who posted....user error....as I mentioned, I am using
some templates. They
offer embed points to insert code. I changed the point at which I was
embedding mysql code
and it works as expected. Jerry, what do you mean by top-posting?

Jul 17 '05 #7
Japhy wrote:
Thanks to all who posted....user error....as I mentioned, I am using
some templates. They
offer embed points to insert code. I changed the point at which I was
embedding mysql code
and it works as expected. Jerry, what do you mean by top-posting?


Because it doesn't follow the normal order of reading.

Why shouldn't you top post?

Putting the response before the question.

What is top posting?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
10
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.