1

I have this code with PHP and HTML but I cant seem to get the PHP code to echo into the HTML.

<?php

countdown(2011,6,7,7,31,0);

function countdown($year, $month, $day, $hour, $minute){
$the_countdown_date=mktime($hour, $minute, 0, $month, $day, $year, -1);
$today=time();
$difference=$the_countdown_date - $today;
$days=floor($difference/60/60/24);
$hours=floor(($difference - $days*60*60*24)/60/60);
$minutes=floor(($difference - $days*60*60*24 - $hours*60*60)/60);
}

?>
<!doctype html>
<html>
<head>

</head>
<body>

<h1><?php echo "$days days $hours hours $minutes minutes";?></h1>

</body>
</html>

Can anyone help? Thanks in advance.

Callum

Callum Whyte
  • 2,379
  • 11
  • 36
  • 55

6 Answers6

6

You are setting the variables $days etc inside a function, but are expecting to access them outside that functions. This cannot be done, as the variables do not have the correct scope.

The smallest change you can make to see it work is this:

function countdown($year, $month, $day, $hour, $minute){
    global $days, $hours, $minutes; // ADD THIS
    $the_countdown_date=mktime($hour, $minute, 0, $month, $day, $year, -1);
    $today=time();
    $difference=$the_countdown_date - $today;
    $days=floor($difference/60/60/24);
    $hours=floor(($difference - $days*60*60*24)/60/60);
    $minutes=floor(($difference - $days*60*60*24 - $hours*60*60)/60);
}

This works because it makes your variables have global scope. However, it's the wrong solution.

The better solution would be to have your function return the values you need -- and since there are three values but only one return, do so with an array:

function countdown($year, $month, $day, $hour, $minute){
    $the_countdown_date=mktime($hour, $minute, 0, $month, $day, $year, -1);
    $today=time();
    $difference=$the_countdown_date - $today;
    $days=floor($difference/60/60/24);
    $hours=floor(($difference - $days*60*60*24)/60/60);
    $minutes=floor(($difference - $days*60*60*24 - $hours*60*60)/60);
    return array($days, $hours, $minutes);
}

And then, call the function and retrieve the values like this:

list($days, $hours, $minutes) = countdown(2011,6,7,7,31,0);

There are many variations on the "return multiple values in an array" theme; I have used the shortest (but perhaps not the most clear, because it uses list) here.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 2
    @Jon - Please bold the "However" sentence. :) – Jared Farrish Mar 31 '11 at 22:26
  • I´m pretty sure your first solution does not work. You need to declare your variables outside the function as well, just using `global` is not enough. But your second solution is better anyway... – jeroen Mar 31 '11 at 22:27
  • @jeroen - That is true. I'd add the global declaration to round it out. I still much prefer the second function to using global variables. – Jared Farrish Mar 31 '11 at 22:29
  • @Jon - Really? If a variable doesn't exist until the function is called with the global keyword, it will automatically create global variables by those names? – Jared Farrish Mar 31 '11 at 22:31
  • @JaredFarrish: Yes. Have you tried it? There's nothing surprising in it -- you just tell the compiler "put these variables up in global scope". There is no magic reason they need to be *already* in global scope. – Jon Mar 31 '11 at 22:32
  • @Jon - Interesting. No, I haven't. – Jared Farrish Mar 31 '11 at 22:32
  • No, it doesn´t work. I just saw a question about that a few days ago: http://stackoverflow.com/questions/5462229/php-newbie-question-global-variables-for-web/5462265#5462265 – jeroen Mar 31 '11 at 22:33
  • @jeroen: I don't know why you think that, maybe this will change your mind? http://ideone.com/nay96 – Jon Mar 31 '11 at 22:36
  • Well, the poster of the question I mentioned (using global did not work) and the php manual: `By declaring $a and $b global within the function, all references to either variable will refer to the global version.`. But pretty sure can still be wrong :-) – jeroen Mar 31 '11 at 22:39
  • 1
    @jeroen: Don't trust the poster of any question more than the compiler. And the manual reference sure does *not* say it won't work. – Jon Mar 31 '11 at 22:41
  • @jeroen - You're telling me... :D – Jared Farrish Mar 31 '11 at 22:46
1

You have to declare your variables in your function as global.

ayk
  • 1,330
  • 2
  • 15
  • 24
  • 1
    This is misleading. The variables need to actually be declared "in global scope" and then accessed using the `global` keyword within the function, according to this answer. However, DO NOT THIS. It is not necessary. Wait for Jon's answer, he'll get you fixed up. – Jared Farrish Mar 31 '11 at 22:23
  • @Callum: DONT do that... return an array of the values you need like `return array('days' => $days, 'hours' => $hours, 'mins' => $minutes);` Or if you dont need to modify the format of the string your outputting just return the string you want to echo form the function. – prodigitalson Mar 31 '11 at 22:24
  • Add global in front of your variables in your function, which you use outside this function. – ayk Mar 31 '11 at 22:27
  • You are right, see Jon's comment on his answer. However, it's still far less than the best solution to the problem, IMO. – Jared Farrish Mar 31 '11 at 22:34
0

You're not returning $days, $hours or $minutes from the function.

<?php

list($days,$hours,$minutes) = countdown(2011,6,7,7,31,0);

function countdown($year, $month, $day, $hour, $minute){
$the_countdown_date=mktime($hour, $minute, 0, $month, $day, $year, -1);
$today=time();
$difference=$the_countdown_date - $today;
$days=floor($difference/60/60/24);
$hours=floor(($difference - $days*60*60*24)/60/60);
$minutes=floor(($difference - $days*60*60*24 - $hours*60*60)/60);
return array($days,$hours,$minutes);
}

?>
<!doctype html>
<html>
<head>

</head>
<body>

<h1><?php echo "$days days $hours hours $minutes minutes";?></h1>

</body>
</html>
Matt
  • 9,068
  • 12
  • 64
  • 84
0
<?php
function countdown($year, $month, $day, $hour, $minute){
$the_countdown_date=mktime($hour, $minute, 0, $month, $day, $year, -1);
$today=time();
$difference=$the_countdown_date - $today;
$days=floor($difference/60/60/24);
$hours=floor(($difference - $days*60*60*24)/60/60);
$minutes=floor(($difference - $days*60*60*24 - $hours*60*60)/60);
return $days.' days '.$hours.' hours '.$minutes.' minutes';
}

?>
<!doctype html>
<html>
<head>

</head>
<body>

<h1><?php echo countdown(2011,6,7,7,31,0);?></h1>

</body>
</html>
  • 1
    In all respect, this makes the function *less* useful, so it's not a good solution. – Jon Mar 31 '11 at 22:31
  • I don't want to argue, but consider: this way, the function can only do *one* thing (return a string). Otherwise (if it returns three numbers), you can then use them in any way you might find useful. – Jon Mar 31 '11 at 22:33
  • @jon well that's a guess on your part that the output may be required in another format, i guessed it wouldn't be, its up to the op which is *right* –  Mar 31 '11 at 22:35
0

Try changing the last line of the function countdown() to return the correct string ("$days days etc.") and then making the embedded line of php code

<h1><?php echo countdown(2011,6,7,7,31,0); ?></h1>

  • This will not work, the function doesn't return the string that's to be output – Matt Mar 31 '11 at 22:26
  • I mentioned to change it so that it does (in the first half of my answer) –  Mar 31 '11 at 22:31
0

Like everyone else said you cant access the variables in the function. A way around this is to have the function return something.

function countdown($year, $month, $day, $hour, $minute){
    $the_countdown_date=mktime($hour, $minute, 0, $month, $day, $year, -1);
    $today=time();
    $difference=$the_countdown_date - $today;
    $days=floor($difference/60/60/24);
    $hours=floor(($difference - $days*60*60*24)/60/60);
    $minutes=floor(($difference - $days*60*60*24 - $hours*60*60)/60);
    return array(
        'days' => $days,
        'hours' => $hours,
        'minutes' => $minutes
    );
}

$cd = countdown(2011,6,7,7,31,0);

<?php echo $days ?> days <?php echo $hours ?> hours <?php echo $minutes ?> minutes
Galen
  • 29,976
  • 9
  • 71
  • 89