Function variables

Thread Solved

Join Date: Jun 2005
Posts: 147
Reputation: ashneet is an unknown quantity at this point 
Solved Threads: 1
ashneet's Avatar
ashneet ashneet is offline Offline
Junior Poster

Function variables

 
0
  #1
Nov 15th, 2005
I made this function and the problem i have is that the Variavle $Time is not available to the whole program and what i mean by that is the function runs but doesn't set the variable to the whole thing and i have tested the function by doing echos.

Is there a way to set the variable in the function usable out side of the function.
[PHP]
function Times()
{
if (time() < mktime(12,00,00))
{
$Time = 'AM';
}
else
{
$Time = 'PM';
}
}
[/PHP]
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Function variables

 
0
  #2
Nov 15th, 2005
Originally Posted by ashneet
I made this function and the problem i have is that the Variavle $Time is not available to the whole program and what i mean by that is the function runs but doesn't set the variable to the whole thing and i have tested the function by doing echos.

Is there a way to set the variable in the function usable out side of the function.
[PHP]
function Times()
{
if (time() < mktime(12,00,00))
{
$Time = 'AM';
}
else
{
$Time = 'PM';
}
}
[/PHP]
By default, all variables used in functions are local variables, even if you use the same name of a global variable. You have to declare them global within the function. You can achieve this by adding this to the start of that function:

[PHP]
global $Time;
[/PHP]

-Fredric
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 147
Reputation: ashneet is an unknown quantity at this point 
Solved Threads: 1
ashneet's Avatar
ashneet ashneet is offline Offline
Junior Poster

Re: Function variables

 
0
  #3
Nov 15th, 2005
Thanks that helps a lot
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 138
Reputation: sarahk is an unknown quantity at this point 
Solved Threads: 1
sarahk's Avatar
sarahk sarahk is offline Offline
Junior Poster

Re: Function variables

 
0
  #4
Nov 16th, 2005
rather than create a global variable I'd have done it this way
function Times()
{
  if (time() < mktime(12,00,00)) $Time = 'AM';
  else $Time = 'PM';
  return $Time;
}

$thisTime = Times();
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC