$result=mysql_query($sql_info) or die("Error 1: ".mysql_error());
$no= mysql_fetch_array($result);
$groups_no = $no['number'];
mysql_num_rows($result) or die("Error 2: ".mysql_error());
// echo "what's going on?"; if I uncomment this line then the echo below executes
if(($this->user_group=="I am Talent") &&($groups_no==0)) {
header("Location: ".MAIN_URL.$this->groups_page);
echo "what's going on?"; //THIS DOESN'T EXECUTE BY ITSELF...WHY NOT??
}
if(mysql_result(mysql_query(sprintf("SELECT last_update FROM `users_profile` WHERE users_id=%d LIMIT 1",$this->id)),0)==0) {
header("Location: ".MAIN_URL.$this->groups_page);
}
echo "what's going on?"; //won't execute unless the line above
有谁知道为什么上面的代码不能工作,除非有一个 echo 语句?我已经通过手动数据库检查确认第一个 if 语句应该执行。
这应该会导致页面重定向到存储在变量 MAIN_URL.$this->groups_page 中的链接(缺乏重定向是最初的问题)。但是,除非 if 语句中或 if 语句上方有回显,否则 header("Location: ...") 部分不会执行。到底为什么会这样?我在 header("Location: ".MAIN_URL.$this->groups_page); 之后放置了一条 echo 语句第一个 if 语句中的行,但它没有执行。仅当我在 header(...) 行之前有 echo 语句时,它才会执行。
最佳答案
该 echo 确实对其他 echo 没有影响。我怀疑正在发生的是第二个回显通常不会执行,因为位置重定向首先发送到浏览器,因此最后一个回显永远不会显示。
但是,当您启用第一个回显,然后将其中一个位置 header 发送到浏览器时,PHP 应该发出错误,指出输出已经开始(通过第一个回显)。在这种情况下,第二个回显可能会显示出来,因为此时无法发送 header 。
@ldiqual 同上;当你的回答出现时,我正在发布这篇文章;)
关于除非在某个位置有回显,否则 PHP 代码不会执行。为什么是这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8498064/