重新开始执行PHP(向后移动)


Restart executing PHP from the beginning (moving backwards)?

当我的PHP中满足某个条件时,我希望执行它上面的东西(一个头)。我已经读过关于ob_start的所有内容,但通过多次(我指的是几天)的测试,我真的看不出有什么不同。所以现在我想知道我是否可以把标题留在顶部,稍后再激活它?

if($x == 1)
    header("Location: something"); exit;
... // echo's, HTML, etc
$x = 1;
restart_php();

可能不会,但值得一试。也许有人知道一种技术。

如果您以后真的想指定标头(并且它是可变的)来提供某种救援重定向。。。试试这个尺寸;

<?php
function restart_php($x){
    if($x == 1)  
        ob_end_clean();   # throw everything away
        header("Location: something"); 
        exit;  
    }else{
        $page = ob_get_contents();
        ob_end_clean();
        echo $page;
    }
}
ob_start();  # everything after here goes into buffer so header is clean
... // echo's, HTML, etc  
$x = 1;  # to previous note on this, X is always equal to 1?
restart_php($x);  # which clears the buffer and either shows header or echo's the contents
?> 

不知道,如果我理解你的问题。。。。在php中(实际上)没有办法使用GO to(就像在basic中一样)。。。。

更新:由于php 5.3.0,实现了goto函数

你能做什么,例如在函数的顶部写下你的条件

<?php
 function shutdown() {
   global $x;
   if($x==1) {
      header("location ...");
   } 
   //.....
 }
 // and then call the register_shutdown_function
 register_shutdown_function('shutdown');
 ?>

有关此功能的更多信息register_shutdown_function()