![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Posts: 27
Rep Power: 0
![]() |
Opposite of global in functions
Hi all,,
Another stupid question from me... I know that in order to use a variable from your main php within a function you need to use global $variable but I was wondering how you can set a variable within a function and get it to carry out into the main PHP. now I know a bit about return but I'm unsure how to return what I need... I thought about returning an array with 1 and my variable instead of true... but it's hard to evaluate when coming out of the function. Here's my code. all my functions are stored in an 'include' file but I have simplified what's going on: php Syntax (Toggle Plain Text)
and this code works fine except for the $daysold bit... it works within the function but outside of the function it does not work... so I thought maybe something like this: (I HAVE LEFT OUT MOST OF THE PHP FOR READABILITY AND ONLY SHOWN THe PARTS I THOUGHT I WOULD EDIT) php Syntax (Toggle Plain Text)
but I presume it would be much easier if I could just set a variable for $daysold within the function like in my first bit of code but have it stay outside the function... I have googled "php functions global" and a load of variations but I cannot find what I need. So is there a command I can use to set a variable within my function and get it to stay out in my main code, or should I use an array and call the function twice, as in my second bit of code? |
|
|
|
|
|
#2 |
|
Banned
![]() ![]() |
Re: Opposite of global in functions
Nope, sorry. That's the whole point of return. To give value(s) obtained from inside a function back to the calling scope.
If you REALLY wanted some way around that, you could define a global and assign it a value from inside the function. Then use this global normally outside the function. So the inverse of what you wanted, except it would behave how you require. There are also more complicated data structures that can make returning multiple values, or passing multiple values between functions easier. But that would just make this whole situation that much more unecessarily complicated for you. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2007
Posts: 27
Rep Power: 0
![]() |
Re: Opposite of global in functions
Thanks for your concise reply. I get it now.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2007
Posts: 27
Rep Power: 0
![]() |
Re: Opposite of global in functions
Although I have just tried what you said and it doesn't seem to work.
here's my code php Syntax (Toggle Plain Text)
$daysold echoes out blank. I suppose I'll just use return to get the info back out |
|
|
|
|
|
#5 |
|
Banned
![]() ![]() |
Re: Opposite of global in functions
Try this. I've never had to do this before though, because I always do it the other way. This is untested.
global $daysold;
$daysold="";
if (CheckDateOld($datestamp,60)){$reportdatefont=$redfont;}
echo "days old=$daysold";
//--------------------------------------------------------------------------------
function CheckDateOld($date,$howlong)
{
$reportdat = explode ("-",$date);
$reportyear = $reportdat[0];
$reportmonth = $reportdat[1];
$reportday = $reportdat[2];
$reportdate = implode ("-",$reportdat);
$usdate = $reportmonth. "/" . $reportday."/" .$reportyear;
$daysold = floor((time() - strtotime($usdate))/86400);
if ($daysold > $howlong){return true;}
} |
|
|
|
|
|
#6 |
|
Banned
![]() ![]() |
Re: Opposite of global in functions
By the way... is there any reason you can't just do something like this?
php Syntax (Toggle Plain Text)
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Nov 2007
Posts: 27
Rep Power: 0
![]() |
Re: Opposite of global in functions
Thanks that does exactly what I need!! I just never thought of it that way
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Nov 2007
Posts: 27
Rep Power: 0
![]() |
Re: Opposite of global in functions
In fact when I checked your code there's no need now for the $howlong paramater passing to the function... so here's my new code..
Thanks again... php Syntax (Toggle Plain Text)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Writing Javascript functions for a Google Gadget? | Writlaus | JavaScript and Client-Side Browser Scripting | 3 | Jul 22nd, 2006 8:43 AM |
| binding between ordinary member functions & polymorphic member functions | ASH | C++ | 2 | May 11th, 2006 5:36 AM |
| Use C++ functions is VB.NET | userName123 | Visual Basic .NET | 1 | Oct 8th, 2005 9:32 AM |
| Exporting Functions | victorsk | Visual Basic | 1 | May 18th, 2005 3:32 PM |
| User-defined creatNode and deleteNode functions for a doubly-linked list | jgs | C | 2 | Apr 28th, 2005 9:53 AM |