如何在不影响php中任何其他函数的情况下清除先前返回的结果?

how would i clear previously echoed result without affecting any other functions in php?

本文关键字:清除 情况下 结果 返回 函数 其他 影响 php 任何      更新时间:2023-09-26

我目前正在网站上工作,它有一个搜索框来搜索特定项目。页面以表格格式回显结果。到目前为止,一切都很完美,但当我试图过滤结果(取决于特征)时,我得到了两组结果。一个是先前显示的结果表,另一个是过滤后的结果。我不希望之前的结果再次显示在屏幕上而不影响任何其他程序。比如会议??我不知道该如何处理这种情况。

<?php
include'search.php';// form for a search box. 

         if (isset($_POST['search_name']))  {
$search_name=mysql_real_escape_string(htmlentities(trim($_POST['search_name'])));
$errors = array();
if (empty($search_name)){
    $errors[] ='please enter a search term';
    }
else if (strlen($search_name)<3){
    $errors[] = 'your search term must be three or more characters'; 
    }
else if (1==2){
    $errors[] ='your search for '.$search_name.' returened no results';
    }
if (empty($errors)){

   filter($search_name); //it display another form in the navigation bar to filter the search result.

   search_results($search_name);//searches for all the result onthe database depending on the keyword  entered in searchbox. 

    } else{
    foreach($errors as $error)  {
        echo $error,'</br>';
        }
    }
}
?>

查看代码:

echo 'world';
echo 'hello !';

可以使用ob_start(), ob_get_contents()ob_clean()来截取回波。

ob_start();
echo 'world';
var $echoed = ob_get_contents();
ob_clean();
// real echo
echo 'hello ' . $echoed . '!';
// now you see
// hello world!

因为ob '输出缓冲'是PHP原生的,所以您可以将它用于函数,包含等任何东西。我使用这种方法来截取控制器流中的(1.)输出,并截取视图的(2.)输出,以便以后可以组合它们(例如,将PHP错误呈现到调试div中。