Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Dec 6th, 2007, 7:11 AM   #1
davil
Newbie
 
Join Date: Nov 2007
Posts: 27
Rep Power: 0 davil is on a distinguished road
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)
  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. $reportdatefixed = strtotime($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)
php Syntax (Toggle Plain Text)
  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?
davil is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
[IMG] code is On
HTML code is Off
Forum Jump

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