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.

function Times()
{
if (time() < mktime(12,00,00))
{
$Time = 'AM';
}
else
{
$Time = 'PM';
}
}

Dani AI

Generated

's issue is the standard PHP function-scope behavior: a variable assigned inside a function is local to that function. 's suggestion to declare the name global will make the outside variable visible, but globals introduce hidden coupling and make code harder to maintain. 's suggestion to have the function produce a value and assign it where needed is the cleaner approach.

A minimal, robust way to get "AM" or "PM" is to have the function return that string rather than try to set a global. PHP also provides built-in formatting so manual comparisons aren't required. For a quick solution, a function that returns the meridian based on the server time keeps scope explicit and testable:

function getMeridian() {
    return date('A');   // yields "AM" or "PM" according to server timezone
}

$meridian = getMeridian();

If the server timezone might not match the desired zone, use a DateTime with an explicit timezone to avoid surprises:

function getMeridianForTZ($tz = null) {
    $zone = $tz ? new DateTimeZone($tz) : new DateTimeZone(date_default_timezone_get());
    return (new DateTime('now', $zone))->format('A');
}

echo getMeridianForTZ('America/New_York');

As an alternative when a caller really needs a variable set rather than a return value, passing by reference can be used, but it is less clear than returning a value. Basic debugging tips: enable full error reporting and inspect variables with var_dump() when testing scope. Overall recommendation: prefer returning values (as suggested) and use DateTime when timezone accuracy matters; avoid globals except for very small, controlled cases.

Recommended Answers

All 3 Replies

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.

function Times()
{
if (time() < mktime(12,00,00))
{
$Time = 'AM';
}
else
{
$Time = 'PM';
}
}

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:

global $Time;

-Fredric

Thanks that helps a lot

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();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.