PHP 函数返回。允许作弊
好的。
我有一个非常长且相当复杂的函数。
它看起来几乎像这样:
<?php
function hello() {
echo 'My Function!' ?>
<ul>
<li> Blablabla </li>
<ul>
(...)
<?php } ?>
这里的巨大问题是我无法回应任何内容。
我的函数必须返回它的内容,而不是回显或直接输出(它必须是这样,它是一个 Wordpress 短代码,当我回显时 - 内容显示在页面顶部 - 总是,而不是在我所在的地方想要它们):
<?php
function hello() {
$output .= 'My Function!';
$output .= '<ul>';
$output .='<li> Blablabla </li>';
$output .='<ul>';
(...)
return $output;
} ?>
我希望到目前为止这很容易。
现在,真正的问题是:
我有大量的直接输入代码,例如:
?>
<div>
<span>
<p>Smth</p>
<a>smth</a>
</span>
</div>
<?php
到处添加 $output 会杀死漂亮的段落/空白,并且代码变得非常难以阅读和理解(并且所有 HTML 元素现在都是变量的一部分,所以即使是我的php 编辑器没有很好地对待它们并将它们着色为 PHP 元素)。
另一件事,我有大量像这样的行:(
<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link">
是的,这是一行)
而且我完全不知道如何将这些行添加到 $output 中。
$output .= '<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link"> ';
当然不起作用(即使在 ' 和 " 之前有 \')。
我相信一定有一种更简单的方法来附加所有要返回的代码,但是如何呢? 我尝试过 ob_start();在代码之前并返回 ob_get_clean();之后,但它输出短代码名称而不是内容。
OK.
I have a very long and pretty complicated function.
It looks almost like this one:
<?php
function hello() {
echo 'My Function!' ?>
<ul>
<li> Blablabla </li>
<ul>
(...)
<?php } ?>
The HUGE problem here is that I'm UNABLE to echo anything.
My function HAVE to return it's contents instead of echoing or direct outputting (it has to be that way, it's a Wordpress shortcode and when I echo - the contents are getting displayed at the top of the page - ALWAYS, not in the place where I want them):
<?php
function hello() {
$output .= 'My Function!';
$output .= '<ul>';
$output .='<li> Blablabla </li>';
$output .='<ul>';
(...)
return $output;
} ?>
I hope it's easy till now.
Now, the real problems are:
I have tons of direct input code like:
?>
<div>
<span>
<p>Smth</p>
<a>smth</a>
</span>
</div>
<?php
Adding $output everywhere kills the nice paragraphs/whitespace and code is getting VERY HARD to read and understand (and all HTML elements are parts of variable now, so even my php editor is not treating them well and coloring them as PHP elements).
And another thing, I have tons of lines like this one:
<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link">
(yes, this is a single line)
And I have absolutely no idea how to add such lines to $output.
$output .= '<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link"> ';
Doesn't work of course (even with \'s before ' and ").
I believe there MUST be an easier way to attach all the code to return, but how?
I've tried with ob_start(); before code and return ob_get_clean(); after, but it outputs shortcode name instead of contents.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很难想象你在这里试图解决的问题是什么 - 尽管我不熟悉 wordpress。难道你不能只调用输出应该去的函数吗?
您可以使用输出缓冲 - 像往常一样使用 echo/print 但是...
但这并不能解决您仍然需要在页面中的正确位置发送到浏览器的问题 - 如果您可以这样做:
在右侧地点,那么你一定可以做到:
在同一个地方。
Its very hard to imagine what the problem you are trying to solve here is - although I'm not familiar with wordpress. Can't you just call the function where the output is supposed to go?
You could use output buffering - use echo/print as usual but...
But that doesn't solve the problem that you still need to send to the browser at the right place in the page - and if you can do:
in the right place, then you can surely do:
in the same place.
我同意 symcbean,但这可能是与 Wordpress 集成的更实用的方法:如果您现在有一个名为 hello( ) 的函数,它显示 HTML,您可能需要考虑将该函数重命名为 hello_content( ) (或类似的名称)并将 hello( ) 函数替换为 symcbean 给您的建议:
这应该可以解决您眼前的问题。
I agree with symcbean, but this might be a more practical approach at integrating with Wordpress: if you now have a single function called hello( ) which displays HTML, you might want to consider renaming that function to hello_content( ) (or something similar) and replace the hello( ) function with the suggestion symcbean gave you:
That should fix your immediate issue.
PHP Heredoc 语法将使事情看起来整洁,并且您可以将输出返回到变量。只要您不需要任何常量,它就可以正常工作。
你可以这样使用它:
PHP Heredoc syntax will keep things looking neat and tidy and you can return the output to a variable. As long as you don't require any constants it will work fine.
You use it in this fashion: