PHP:使用适当缩进与Heredocs [英] PHP: Using Proper Indentation with Heredocs

查看:114
本文介绍了PHP:使用适当缩进与Heredocs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚读了 heredocs 但我没有看到任何方式意图的代码正确。这是可能在PHP吗?

I just read thought php doucmentation for heredocs but I did not see any way to intent the code properly. Is this possible in php?

现在我正在这样做,但这对可读性有害。

Right now I am doing this, but this is bad for readability.

<?php

        if(something){
            ...
            echo <<< END      
                    This is a test.  I am writing this
                    text out.  
END; 
        } # end of if statment

我想有这样的: p>

I would like to have something like this:

<?php

        if(something){
            ...
            echo <<< END      
                    This is a test.  I am writing this
                    text out.  
            END; 
        } # end of if statment

我知道bash有一个方法虽然我不记得它是什么),所以我想知道是否可能在PHP。我不这么认为,但我以为我会问。

I know that bash has a method to do this (although I cannot remember what it is), so I was wondering if it was possible to do in php. I don't think so but I thought I would ask.

推荐答案

PHP正确格式化Heredoc语句的限制。这是一个解析器限制。正如文档所述:

It's a limitation of PHP to properly format Heredoc statements. It's a parser limitation. As the documentation states:


这是非常重要的是注意
行与关闭标识符必须
没有其他字符,除了
可能是一个分号(;)。这意味着
,特别是标识符可能不是
缩进,并且在
分号之前或之后可能没有任何
空格或制表符。同样重要的是
意识到在关闭标识符之前的第一个字符
必须是
由本地
操作系统定义的换行符。这是UNIX
系统上的\\\
,包括Mac OS X.
结束分隔符(可能后跟
由分号)也必须跟随
换行。

It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.

如果此规则损坏
且关闭标识符不是
clean,则不会被视为
关闭标识符,PHP将
继续寻找。如果在
之前没有找到正确的
关闭标识符,则当前文件结束时,将在最后一行产生parse
错误。

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

这是未知的,如果这将是未来的PHP解决。

It's unknown if this is gonna be resolved in the future of PHP.

这篇关于PHP:使用适当缩进与Heredocs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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