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

2024-04-13php开发问题
13

本文介绍了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

我想要这样的东西:

<?php

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

我知道 bash 有一个方法可以做到这一点(虽然我不记得它是什么),所以我想知道是否可以在 php.ini 中做到这一点.我不这么认为,但我想我会问.

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.

推荐答案

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

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 on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.

如果这条规则被打破并且结束标识符不是干净",它不会被视为关闭标识符,PHP 将继续找一个.如果一个适当的之前未找到关闭标识符当前文件的结尾,一个解析错误将出现在最后一行.

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 中使用正确的缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17