【问题标题】:Best way to determine if a function has an output?确定函数是否有输出的最佳方法?
【发布时间】:2016-12-30 20:13:04
【问题描述】:

我有一个函数列表,它们运行一个相当深的例程来确定从哪个 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 是否返回?谢谢。真的很感激!

【问题讨论】:

  • 在我写这篇文章时,我已经可以看到,运行 check less 的最佳方法可能是将我的输出函数合并到一个 output_header 函数中。然后我可以检查一次,针对那个函数而不是针对 3 个不同的函数。但我仍然想知道检查函数是否返回和输出的最佳方法。 - 谢谢!欣赏它。
  • 链接的问答可能是您想要做的。只需捕获函数的输出并检查它是否为空。
  • 谢谢。 2 件事。我想知道是否可以检查我的变量 $content 是否返回,因为如果不是,那么显然没有要输出的内容,所以我基于此运行我的 html 包装器。那可能吗?或者按照您所说的,可以在类中的函数之外打开输出缓冲吗?这样我就可以在我的三个标题输出之前将其打开并在之后将其关闭,而不是在每个函数中打开和关闭它。我环顾四周,没有看到任何说明或表明这是否可以或具体可以在课堂上完成的内容? @Rizier123
  • 不,您不能只检查返回值,因为如果有输出,您将无法再捕获它。是的,您可以在方法内的类中启动输出缓冲,调用您的函数,然后检查您是否捕获了某些内容。
  • @Rizier123 太棒了!!!谢谢!我明白那个。我不知道 $f = function_name 和 $p = params。这很有帮助,哈哈。现在是有道理的。谢谢!如果您认为将您的评论作为答案是有效的,我可以将其标记为已回答。谢谢!

标签: php wordpress wordpress-theming output


【解决方案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() 以检查变量内容。 之后你就可以使用 $output 的内容了。

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

您也可以使用 isset() 之类的函数来确定变量是否不是null

http://php.net/isset

http://php.net/empty

【讨论】:

  • 太棒了!谢谢你。经测试,效果很好。很简单,也很简单:) 我写了一个比较 TRUE 和 FALSE 的例子 @gist.github.com/NoahjChampion/c83bfcdd6270397261ac5b7458d797cc - 对于我自己,如果你愿意,可以随时用该代码更新你的答案。不管怎样,谢谢! :) 欣赏它! @GxTruth
猜你喜欢
  • 2016-02-14
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 2013-02-07
  • 2010-09-17
  • 1970-01-01
相关资源
最近更新 更多