PHP: Using Proper Indentation with Heredocs [duplicate]

主宰稳场 提交于 2019-11-28 07:21:14

问题


This question already has an answer here:

  • HEREDOC interfering with code indentation 6 answers

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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4068556/php-using-proper-indentation-with-heredocs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!