我有一些想要转换为函数的 PHP 代码。不幸的是,该代码使用 nowdoc 为 sprintf() 调用创建格式字符串。这带来了格式上的困境,因为这意味着我不能缩进结束标识符以匹配函数布局。nowdoc 是一个 HTML 片段,因此我可以删除所有换行符并将其视为一个简单的字符串变量,但这会使代码更难阅读。另一种方法是使用 nowdoc 在全局范围内创建格式字符串,并将生成的字符串传递给函数。有什么方法可以使用 nowdoc 并保持我的正常格式样式?
问问题
110 次
1 回答
0
正如我在评论中提到的,如果您想在两个页面上都保留格式,只需使用include()
:
/string.php
<?php
$str = <<<'EOD'
This is a string
EOD;
// This line after "EOD;" needs something on it or it may throw this error:
// Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN)
/myfunction.php
<?php
function myfunction()
{
# Include the nowdoc file
include(__DIR__.'/string.php');
# Return the string back
return $str;
}
于 2018-08-12T03:39:15.503 回答