简体   繁体   English

Heredoc在PHP中不起作用

[英]Heredoc Not Working in PHP

I am learning to build web applications with PHP. 我正在学习使用PHP构建Web应用程序。 However, I keep having issues using heredocs to stick some HTML into the page from within PHP. 但是,在使用heredocs将一些HTML从PHP粘贴到页面中时,我一直遇到问题。 Basically, I wanna do something like 基本上,我想做类似的事情

echo $htmlPage;

after PHP does some data processing and stuff. PHP完成一些数据处理和工作之后。 But, as the title suggests, its not working for some reason. 但是,正如标题所示,由于某种原因,它不起作用。 And I have reason to believe it has something to do with how I made the heredoc, which can be seen below. 而且我有理由相信,这与我制作Heredoc的方式有关,如下所示。

Thanks 谢谢

$htmlPage = <<<HTML
            <style>
                .signup
                {
                    border: 1px solid #999999; 
                    font: normal 14px helvatica; 
                    color: #444444; 
                }
            </style>

            <script src="form_validation.js">

            </script>

        </head>
        <body>

            <table border="0" cellpadding="2" cellspacing="5" bgcolor="#eeeeee">
                <th cellspan="2" align="center">Sign Up Form</th>
                <form method="post" action="addUser.php" onsubmit="return validate(this)">
                    <tr> 
                        <td>Forename</td>
                        <td><input type = "text" maxlength="32" name="forename"/></td>
                    </tr>

                    <tr> 
                        <td>Surname</td>
                        <td><input type = "text" maxlength="32" name="surname" /></td>
                    </tr>

                    <tr> 
                        <td>Username</td>
                        <td><input type = "text" maxlength="16" name="username" /></td>
                    </tr>

                    <tr> 
                        <td>Password</td>
                        <td><input type = "password" maxlength="12" name="password" /></td>
                    </tr>

                    <tr> 
                        <td>Age</td>
                        <td><input type = "text" maxlength="3" name="age" /></td>
                    </tr>

                    <tr> 
                        <td>E-mail</td>
                        <td><input type = "text" maxlength="32" name="email" /></td>
                    </tr>

                    <tr> 
                        <td colspace="2" align="center">
                        <td><input type = "submit" value="Sign Up"/></td>
                    </tr>

                </form>
            </table>

        </body>
    </html>
HTML; 

You have a trailing space and no new line after the closing delimiter per the manual both are required. 根据手册,在结束定界符后您必须有一个尾随空格并且没有新行。

It is very important to note that the line with the closing identifier must contain no other characters, except 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 must also be followed by a newline. 在包括Mac OS X在内的UNIX系统上,这是\\ n。结束定界符也必须后跟换行符。

- http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc -http://php.net/manual/zh-CN/language.types.string.php#language.types.string.syntax.heredoc

Error reporting should have thrown an unexpected end of file . 错误报告应该抛出了unexpected end of file Here's a functional example: 这是一个功能示例:

$htmlPage = <<<HTML
            <style>
                .signup
                {
                    border: 1px solid #999999; 
                    font: normal 14px helvatica; 
                    color: #444444; 
                }
            </style>

            <script src="form_validation.js">

            </script>

        </head>
        <body>

            <table border="0" cellpadding="2" cellspacing="5" bgcolor="#eeeeee">
                <th cellspan="2" align="center">Sign Up Form</th>
                <form method="post" action="addUser.php" onsubmit="return validate(this)">
                    <tr> 
                        <td>Forename</td>
                        <td><input type = "text" maxlength="32" name="forename"/></td>
                    </tr>

                    <tr> 
                        <td>Surname</td>
                        <td><input type = "text" maxlength="32" name="surname" /></td>
                    </tr>

                    <tr> 
                        <td>Username</td>
                        <td><input type = "text" maxlength="16" name="username" /></td>
                    </tr>

                    <tr> 
                        <td>Password</td>
                        <td><input type = "password" maxlength="12" name="password" /></td>
                    </tr>

                    <tr> 
                        <td>Age</td>
                        <td><input type = "text" maxlength="3" name="age" /></td>
                    </tr>

                    <tr> 
                        <td>E-mail</td>
                        <td><input type = "text" maxlength="32" name="email" /></td>
                    </tr>

                    <tr> 
                        <td colspace="2" align="center">
                        <td><input type = "submit" value="Sign Up"/></td>
                    </tr>

                </form>
            </table>
        </body>
    </html>
HTML;
echo $htmlPage;

Demo: https://3v4l.org/bEI2B 演示: https//3v4l.org/bEI2B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM