PHP:在 Heredocs 中使用正确的缩进

发布于 2024-09-29 17:03:16 字数 811 浏览 1 评论 0原文

我刚刚阅读了 heredocs 但我没有看到任何方法来正确地意图代码。这在 php 中可能吗?

现在我正在这样做,但这不利于可读性。

<?php

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

我想要这样的东西:

<?php

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

我知道bash有一个方法可以做到这一点(虽然我不记得它是什么),所以我想知道是否可以在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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过气美图社 2024-10-06 17:03:16

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

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

如果违反此规则
并且结束标识符不是
“干净”,不会被视为
结束标识符,PHP 将
继续寻找一个。如果一个适当的
之前未找到结束标识符
当前文件的末尾,解析
最后一行将导致错误。

尚不清楚这个问题是否会在 PHP 的未来得到解决。

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

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.

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.

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

聽兲甴掵 2024-10-06 17:03:16

AFAIK,这是不可能的,您需要放置结束定界符标识符,没有任何空格/制表符/缩进:(

AFAIK, that's not possible, you need to put closing heredoc identifier without any spaces/tabs/indentation :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文