2016-08-23 89 views
7

我有一个函数列表,它运行一个相当深的例程,以确定从哪个post_id获取其内容并将其输出到网站的前端。确定函数是否有输出的最佳方法?

当这个函数返回它的内容时,我希望它被包装在一个html包装器中。我希望这个html包装只有在函数有返回输出时才加载。

在例子中,我有以下...

public static function output_*() { 
    // my routines that check for content to output precede here 
    // if there IS content to output the output will end in echo $output; 
    // if there is NO content to output the output will end in return; 
} 

在充分的解释,我有以下...

如果这些函数之一返回输出我希望它被包裹在一个HTML,所以理论上是这样的就是我试图完成......

public static function begin_header_wrapper() { 
    // This only returns true if an output function below returns content, 
    // which for me is other than an empty return; 
    include(self::$begin_header_wrapper); 
} 

public static function output_above_header() { 
    // my routines that check for content to output precede here 
    // if there is content to return it will end in the following statement 
    // otherwise it will end in return; 
    include($begin_markup); // This is the BEGIN html wrapper for this specifc output 
    // It is, so let's get this option's post id number, extract its content, 
    // run any needed filters and output our user's selected content 
    $selected_content = get_post($this_option); 
    $extracted_content = kc_raw_content($selected_content); 
    $content = kc_do_shortcode($extracted_content); 
    echo $content; 
    include($end_markup); // This is the END html wrapper for this specifc output 
} 
public static function output_header() { 
    // the same routine as above but for the header output 
} 
public static function output_below_header() { 
    // the same routine as above but for the below header output 
} 

public static function end_header_wrapper() { 
    // This only returns true if an output function above returns content, 
    // which for me is other than an empty return; 
    include(self::$end_header_wrapper); 
} 

我现在知道了,时间提前如果其中一个输出函数具有输出,我不想确定两次(一次在开始,一次在末尾),但应该有一种方法可以通过一次检查来完成此操作,但是我想要开始这个兔子洞,并找出确定我的函数是否返回的最好方法。

或者如果有一个完全更好的方法来接近这个请全力以赴,大声笑,让我知道。

我在网上看了这篇文章和其他 @Find out if function has any output with php

所以最后,我只是想知道是否有更好的方式来处理这一点,什么是真正的你想检查的最佳途径我的函数有一个输出返回,所以我可以运行我的HTML包装基于这些条件?

ob_get_length是最好的方法吗?当我查看ob的目的时,这个看起来最好,也是最简单的,但想得到一些建议和反馈。或者,也许我可以检查我的变量$content是否被返回?谢谢。真的很感激它!

+1

当我写这我也能看到,也许到运行检查少是我的输出功能结合成一个output_header功能的最佳途径。然后我可以检查一次,而不是针对3种不同的功能。但我仍然想知道检查函数返回和输出与否的最佳方法。 - 谢谢!欣赏它。 –

+2

这可能与你想要做的事情有关。只需捕获函数的输出并检查它是否为空。 – Rizier123

+0

谢谢。 2件事。我想知道是否可以检查我的变量$ content是否被返回,因为如果它不是,那么显然没有内容输出,所以我运行我的html包装器。那可能吗?或者用你所说的,可以输出缓冲在一个类内的一个函数之外打开吗?这样我可以在我的三个头文件输出之前打开它,并在之后关闭它,而不是在每个函数中打开和关闭它。我环顾四周,没有看到任何说明或显示这是否可以通过班级完成的具体内容? @ Rizier123 –

回答

1

您可以捕获结果并将其存储在一个变量中,然后将该变量赋给empty()函数。

if(!empty(($output = yourFunctionToTest(param1, paramN)))) { 
    // do something with $output (in this case there is some output 
    // which isn't considered "empty" 
} 

这将执行您的函数,将输出存储在一个变量(在这种情况下为$ output)并执行empty()以检查变量内容。 您可以使用之后的$输出内容。

请注意,empty()将空字符串或0视为“空”,因此返回true

作为替代方案,您可以使用函数isset()来确定变量是否不是null

http://php.net/isset

http://php.net/empty

+0

太棒了!谢谢你。测试过,效果很好。简单,轻也:)我写了一个例子比较TRUE与FALSE在gist @ https://gist.github.com/NoahjChampion/c83bfcdd6270397261ac5b7458d797cc - 对于我自己,并随时更新您的答案与该代码,如果你想。无论哪种方式,谢谢! :)欣赏它! @GxTruth –

相关问题