943,514 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3173
  • PHP RSS
Nov 15th, 2005
0

Function variables

Expand Post »
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]
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
ashneet is offline Offline
147 posts
since Jun 2005
Nov 15th, 2005
0

Re: Function variables

Quote 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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Nov 15th, 2005
0

Re: Function variables

Thanks that helps a lot
Reputation Points: 10
Solved Threads: 1
Junior Poster
ashneet is offline Offline
147 posts
since Jun 2005
Nov 16th, 2005
0

Re: Function variables

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();
Reputation Points: 10
Solved Threads: 1
Junior Poster
sarahk is offline Offline
138 posts
since Apr 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need some Help with PHP Craps game!
Next Thread in PHP Forum Timeline: Please HELP! 3 Problems with Form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC