global variable in function

This is a discussion on global variable in function within the PHP Language forums, part of the PHP Programming Forums category; Hello, I'm a newbie using PHP. Having some trouble using a variable within a function. I use some mysql ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-14-2005
Japhy
 
Posts: n/a
Default 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

Reply With Quote
  #2 (permalink)  
Old 05-14-2005
Andy Hassall
 
Posts: n/a
Default Re: global variable in function

On 14 May 2005 08:50:03 -0700, "Japhy" <japhyrider2005@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 / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Reply With Quote
  #3 (permalink)  
Old 05-14-2005
Geoff Berrow
 
Posts: n/a
Default Re: global variable in function

I noticed that Message-ID: <hf8c81h348758kvt6864vpc8lp5otl7n5g@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/
Reply With Quote
  #4 (permalink)  
Old 05-14-2005
Andy Hassall
 
Posts: n/a
Default Re: global variable in function

On Sat, 14 May 2005 17:48:29 +0100, Geoff Berrow <blthecat@ckdog.co.uk> wrote:

>I noticed that Message-ID: <hf8c81h348758kvt6864vpc8lp5otl7n5g@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 / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Reply With Quote
  #5 (permalink)  
Old 05-14-2005
Japhy
 
Posts: n/a
Default Re: global variable in function

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" <japhyrider2005@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 / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis

tool

Reply With Quote
  #6 (permalink)  
Old 05-14-2005
Jerry Stuckle
 
Posts: n/a
Default Re: global variable in function

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.
jstucklex@attglobal.net
==================
Reply With Quote
  #7 (permalink)  
Old 05-15-2005
Japhy
 
Posts: n/a
Default Re: global variable in function

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?

Reply With Quote
  #8 (permalink)  
Old 05-15-2005
Jerry Stuckle
 
Posts: n/a
Default Re: global variable in function

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.
jstucklex@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:33 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0