2016-07-28 124 views
-1

尽我有限的知识和能力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> 

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

+1

谢谢@JonathanKuhn,我觉得它必须是愚蠢的,把你的评论放在答案中,我会给你一些代表。另外,感谢您向我介绍该网站。 – ShemSeger

回答

1

如果您运行的程序出错报告并设置为显示所有错误,问题就会变得更加清晰。请参阅this demo及其引发的错误。这个问题是范围界定的问题之一。 PHP的功能范围意味着在函数外部定义的变量在函数内是不可见的。您应该将您的价值作为参数传递给函数,或者将错误的路径传递给您的函数并将其声明为全局函数。

相关问题