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

0

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

11 Comments

@Jon - Please bold the "However" sentence. :)
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 - That is true. I'd add the global declaration to round it out. I still much prefer the second function to using global variables.
@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?
@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.
|
1

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

4 Comments

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.
@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.
Add global in front of your variables in your function, which you use outside this function.
You are right, see Jon's comment on his answer. However, it's still far less than the best solution to the problem, IMO.
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>

Comments

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>

3 Comments

In all respect, this makes the function less useful, so it's not a good solution.
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 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
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>

2 Comments

This will not work, the function doesn't return the string that's to be output
I mentioned to change it so that it does (in the first half of my answer)
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.