在HEREDOC字符串中调用PHP函数 [英] Calling PHP functions within HEREDOC strings

查看:109
本文介绍了在HEREDOC字符串中调用PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,HEREDOC字符串声明对于输出html块确实非常有用.您可以通过在变量前面加上$来对变量进行解析,但是对于更复杂的语法(例如$ var [2] [3]),则必须将表达式放在{}中.

In PHP, the HEREDOC string declarations are really useful for outputting a block of html. You can have it parse in variables just by prefixing them with $, but for more complicated syntax (like $var[2][3]), you have to put your expression inside {} braces.

在PHP 5中, 实际上可以在HEREDOC字符串内的{}括号内进行函数调用,但是您需要做一些工作.函数名称本身必须存储在变量中,并且您必须像调用动态命名函数一样对其进行调用.例如:

In PHP 5, it is possible to actually make function calls within {} braces inside a HEREDOC string, but you have to go through a bit of work. The function name itself has to be stored in a variable, and you have to call it like it is a dynamically-named function. For example:

$fn = 'testfunction';
function testfunction() { return 'ok'; }
$string = <<< heredoc
plain text and now a function: {$fn()}
heredoc;

如您所见,这不仅仅只是一点点混乱:

As you can see, this is a bit more messy than just:

$string = <<< heredoc
plain text and now a function: {testfunction()}
heredoc;

除了第一个代码示例外,还有其他方法,例如突破HEREDOC来调用该函数,或反转问题并执行类似的操作:

There are other ways besides the first code example, such as breaking out of the HEREDOC to call the function, or reversing the issue and doing something like:

?>
<!-- directly output html and only breaking into php for the function -->
plain text and now a function: <?PHP print testfunction(); ?>

后者的缺点是将输出直接放入输出流中(除非我使用输出缓冲),这可能不是我想要的.

The latter has the disadvantage that the output is directly put into the output stream (unless I'm using output buffering), which might not be what I want.

所以,我的问题的实质是:有没有更优雅的方法来解决这个问题?

So, the essence of my question is: is there a more elegant way to approach this?

基于响应进行当然,某种模板引擎确实会使我的生活变得更加轻松,但是这实际上需要我反转惯用的PHP样式.并不是那是一件坏事,但这可以解释我的惯性.虽然我正在寻找使生活变得更轻松的方法,所以我现在正在研究模板.

Edit based on responses: It certainly does seem like some kind of template engine would make my life much easier, but it would require me basically invert my usual PHP style. Not that that's a bad thing, but it explains my inertia.. I'm up for figuring out ways to make life easier though, so I'm looking into templates now.

推荐答案

我个人不会为此使用HEREDOC.它只是不能构成一个好的模板构建"系统.您所有的HTML都被锁定在一个字符串中,这有几个缺点

I would not use HEREDOC at all for this, personally. It just doesn't make for a good "template building" system. All your HTML is locked down in a string which has several disadvantages

  • 所见即所得没有选项
  • 没有来自IDE的HTML代码完成
  • 将输出(HTML)锁定到逻辑文件
  • 您最终不得不像现在尝试使用hack一样,以实现更复杂的模板,例如循环

获得基本的模板引擎,或者仅将PHP与include一起使用-这就是为什么该语言具有<?php?>分隔符的原因.

Get a basic template engine, or just use PHP with includes - it's why the language has the <?php and ?> delimiters.

template_file.php

<html>
<head>
  <title><?php echo $page_title; ?></title>
</head>
<body>
  <?php echo getPageContent(); ?>
</body>

index.php

<?php

$page_title = "This is a simple demo";

function getPageContent() {
    return '<p>Hello World!</p>';
}

include('template_file.php');

这篇关于在HEREDOC字符串中调用PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆