Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Opposite of global in functions (http://www.programmingforums.org/showthread.php?t=14701)

davil Dec 6th, 2007 7:11 AM

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:
:

  1. $redfont = "<font color='#FF0000'>";
  2.  
  3.  
  4. //example datestamp... usually it would pull loads of these in this form from a MySQL database.
  5. $datestamp="2007-12-25";
  6.  
  7. //--------------------------------------------
  8. // Fix Report date for Irish people etc.
  9. $irishdate=FixDateStamp($datestamp,1);
  10. $usdate=FixDateStamp($datestamp,2);
  11. // --------------------------------------------------
  12.  
  13. // how old is report?
  14. if (CheckDateOld($datestamp,60)){$reportdatefont=$redfont;}
  15.  
  16. $todaysdate = date('l dS \o\f F Y');
  17.  
  18.  
  19. echo "REPORT DATE = <B>$reportdatefont$irishdate [ Report is $daysold days old ]</b><br>";
  20. echo "TODAYS DATE = $todaysate";
  21.  
  22.  
  23.  
  24.  
  25. //end of Primary PHP
  26.  
  27. //------------------------------------ FUNCTIONS -----------------------------------------------------------------------------
  28.  
  29. function FixDateStamp($dategiven,$type)
  30. {
  31. if ($type==1){
  32.         $reportdat = explode ("-",$dategiven);
  33.         $reportyear = $reportdat[0];
  34.         $reportmonth = $reportdat[1];
  35.         $reportday = $reportdat[2];
  36.         $reportdate = implode ("-",$reportdat);
  37.         $reportdateus$reportmonth. "/" . $reportday."/" .$reportyear;
  38.         $reportdatefixedstrtotime($reportdateus);
  39.         $datefixedone = date('l dS \o\f F Y', $reportdatefixed);
  40.         return $datefixedone;
  41.               }
  42. elseif ($type==2){
  43. $reportdat = explode ("-",$dategiven);
  44.         $reportyear = $reportdat[0];
  45.         $reportmonth = $reportdat[1];
  46.         $reportday = $reportdat[2];
  47.         $reportdate = implode ("-",$reportdat);
  48.         $reportdateus$reportmonth. "/" . $reportday."/" .$reportyear;
  49.         return $reportdateus;
  50.                 }
  51. else{
  52.         return "-na-";
  53.     }
  54. }
  55. //--------------------------------------------------
  56. function CheckDateOld($date,$howlong)
  57. {
  58.         $reportdat = explode ("-",$date);
  59.         $reportyear = $reportdat[0];
  60.         $reportmonth = $reportdat[1];
  61.         $reportday = $reportdat[2];
  62.         $reportdate = implode ("-",$reportdat);
  63.         $usdate$reportmonth. "/" . $reportday."/" .$reportyear;
  64.         $daysold = floor((time() - strtotime($usdate))/86400);
  65.         if ($daysold > $howlong){return true;}
  66. }
  67.  
  68.  
  69. //-----------------------------------------------------------------------------------------------------------------------------


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)
:

  1. // how old is report?
  2. if (CheckDateOld($datestamp,60)[0]==1){$reportdatefont=$redfont;}
  3.  
  4. //and
  5.  
  6. echo "Report is ".CheckDateOld($datestamp,60)[1]." days old";
  7.  
  8. //--------------------------------------------------------------------
  9. function CheckDateOld($date,$howlong)
  10. {
  11.         $daysarray=array();
  12.         $reportdat = explode ("-",$date);
  13.         $reportyear = $reportdat[0];
  14.         $reportmonth = $reportdat[1];
  15.         $reportday = $reportdat[2];
  16.         $reportdate = implode ("-",$reportdat);
  17.         $usdate$reportmonth. "/" . $reportday."/" .$reportyear;
  18.         $daysold = floor((time() - strtotime($usdate))/86400);
  19.         $daysarray[1]=$daysold;
  20.         if ($daysold > $howlong){$daysarray[0]=1;}
  21.         return $daysarray;
  22. }


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?

Sane Dec 7th, 2007 12:03 AM

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.

davil Dec 7th, 2007 3:58 AM

Re: Opposite of global in functions
 
Thanks for your concise reply. I get it now.

davil Dec 7th, 2007 4:07 AM

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
:

  1. $daysold="";
  2.  
  3. if (CheckDateOld($datestamp,60)){$reportdatefont=$redfont;}
  4. echo "days old=$daysold";
  5. //--------------------------------------------------------------------------------
  6. function CheckDateOld($date,$howlong)
  7. {
  8.         global $daysold;
  9.         $reportdat = explode ("-",$date);
  10.         $reportyear = $reportdat[0];
  11.         $reportmonth = $reportdat[1];
  12.         $reportday = $reportdat[2];
  13.         $reportdate = implode ("-",$reportdat);
  14.         $usdate$reportmonth. "/" . $reportday."/" .$reportyear;
  15.         $daysold = floor((time() - strtotime($usdate))/86400);
  16.         if ($daysold > $howlong){return true;}
  17. }


$daysold echoes out blank.

I suppose I'll just use return to get the info back out

Sane Dec 7th, 2007 7:54 AM

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;}
}


Sane Dec 7th, 2007 11:01 AM

Re: Opposite of global in functions
 
By the way... is there any reason you can't just do something like this?

:

  1. define('INVALID_DATE', -1);
  2.  
  3. function CheckDateOld($date,$howlong)
  4. {
  5.         $reportdat = explode ("-",$date);
  6.         $reportyear = $reportdat[0];
  7.         $reportmonth = $reportdat[1];
  8.         $reportday = $reportdat[2];
  9.         $reportdate = implode ("-",$reportdat);
  10.         $usdate$reportmonth. "/" . $reportday."/" .$reportyear;
  11.         $daysold = floor((time() - strtotime($usdate))/86400);
  12.         if ($daysold > $howlong) {
  13.             return $daysold;
  14.         }
  15.         else {
  16.             return INVALID_DATE;
  17.         }
  18. }
  19.  
  20. $daysold = CheckDateOld($datestamp,60)
  21. if ($daysold != INVALID_DATE){
  22.     $reportdatefont=$redfont;
  23.     echo "Days Old: $daysold";
  24. }
  25. else {
  26.     // Do something else?
  27. }


davil Dec 12th, 2007 7:51 AM

Re: Opposite of global in functions
 
Thanks that does exactly what I need!! I just never thought of it that way

davil Dec 19th, 2007 5:40 AM

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...

:

  1. function CheckDateOld($date)
  2.  
  3. {
  4. $reportdat = explode ("-",$date);
  5. $reportyear = $reportdat[0];
  6. $reportmonth = $reportdat[1];
  7. $reportday = $reportdat[2];
  8. $reportdate = implode ("-",$reportdat);
  9. $usdate = $reportmonth. "/" . $reportday."/" .$reportyear;
  10. $daysold = floor((time() - strtotime($usdate))/86400);
  11. return $daysold;
  12. }
  13.  
  14.  
  15.  
  16.  
  17. $redfont="<font color='#FF0000'><b>";
  18. $redfont="<font color='#0000FF'>";
  19.  
  20. $daysold = CheckDateOld($datestamp);
  21. if ($daysold>60){$reportdatefont=$redfont;}else{ $reportdatefont=$bluefont; }
  22. echo "$reportdatefont$daysold</b>";



All times are GMT -5. The time now is 3:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC