将HTML代码与PHP结合使用

时间:2011-03-31 22:19:17

标签: php html

我有PHP和HTML的代码,但我似乎无法让PHP代码回显到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>

有人可以帮忙吗?提前谢谢。

卡勒姆

6 个答案:

答案 0 :(得分:6)

您正在函数内设置变量$days等,但希望在该函数之外访问它们。这是不可能的,因为变量没有正确的scope

您可以做出的最小改变就是:

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);
}

这是有效的,因为它使您的变量具有全局范围然而,这是错误的解决方案。

更好的解决方案是让你的函数返回你需要的值 - 并且由于有三个值但只有一个返回,所以使用数组:

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);
}

然后,调用该函数并检索如下值:

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

“返回数组中的多个值”主题有很多变化;我在这里使用了最短的(但也许不是最清楚的,因为它使用了list)。

答案 1 :(得分:1)

您必须将函数中的变量声明为全局变量。

答案 2 :(得分:0)

您未从该函数返回$days$hours$minutes

<?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>

答案 3 :(得分: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>

答案 4 :(得分:0)

尝试更改函数countdown()的最后一行以返回正确的字符串(“$ days days etc.”),然后制作嵌入式的php代码

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

答案 5 :(得分: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' => $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