Php 为什么我的变量没有定义?

Php 为什么我的变量没有定义?,php,if-statement,math,var,strtotime,Php,If Statement,Math,Var,Strtotime,就我有限的PHP知识和能力而言,以下代码应该可以工作: <?php ////DISPLAY DATE OF NEXT COUNCIL MEETING//// $now = date('U'); //get current time $firstTues = strtotime("-1 month first Tuesday 4pm"); //get first Tuesday of the month $secondTues = strtotime("-1 month second Tu

就我有限的PHP知识和能力而言,以下代码应该可以工作:

<?php
////DISPLAY DATE OF NEXT COUNCIL MEETING////

$now = date('U'); //get current time
$firstTues = strtotime("-1 month first Tuesday 4pm"); //get first Tuesday of the month
$secondTues = strtotime("-1 month second Tuesday 5pm"); //get second Tuesday of the month
$fourthTues = strtotime("-1 month fourth Tuesday 5pm"); //get forth Tuesday of the month
$nextTues = strtotime("first Tuesday 4pm"); //get first Tuesday of next month

function nextCouncilMeeting() {

//If todays date less than 1st Tuesday at 11pm, display date for 1st Tuesday 4pm.
if ($now < $firstTues) {
    echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A', $firstTues);
}

//If todays date greater than 1st Tuesday 5pm and less than 2nd Tuesday 11pm, display date for 2nd Tuesday 5pm
elseif ($now > $firstTues and $now < $secondTues) {
    echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A', $secondTues);
}

//If todays date greater than 2nd Tuesday 5pm and less that 4th Tuesday 11pm, display date for 4th Tuesday 5pm
elseif ($now > $secondTues and $now < $fourthTues) {
    echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A', $fourthTues);
} 

//If todays date greater than 4th Tuesday
elseif ($now > $fourthTues){
    echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A', $nextTues);
}
else{
    echo "foobar";
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id="test">
Current Time: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$now); echo " " . $now;?></br>
First Tuesday: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$firstTues);echo " " . $firstTues;?></br>
Second Tuesday: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$secondTues);echo " " . $secondTues;?></br>
Fourth Tuesday: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$fourthTues);echo " " . $fourthTues;?></br>
Next Month First Tuesday: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$nextTues);echo " " . $nextTues;?>
</p>
<h2>Next Council Meeting:</h2>
<h1><?php nextCouncilMeeting()?></h1>
</body>
</html>

当前时间:
第一个星期二:
第二个星期二:
第四个星期二:
下个月第一个星期二:

下次立法会会议:

但是我的变量抛出了一个未定义的错误,我做错了什么?

如果您在运行程序时打开了错误报告并设置为显示所有错误,那么问题就变得更清楚了。请参阅及其引发的错误。问题在于范围界定。PHP具有函数作用域,这意味着在函数外部定义的变量在函数内部不可见。你应该将你的值作为参数传递到函数中,或者走错误的路线,在你的函数中将它们声明为全局值。

谢谢@JonathanKuhn,我想这一定是愚蠢的,把你的评论放在答案中,我会给你一些代表。另外,谢谢你介绍我到那个网站。