dongliang2005 2016-05-28 15:59
浏览 93
已采纳

PHP heredoc赋值给变量错误

I have the following code to display form by a variable using heredoc. I get the following error;

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\PHP\2000.php on line 38

Line 38 is: <form method="POST" action="$_SERVER['PHP_SELF']">

<?php

    if ( !$_POST ){

            $display_block = <<<EOB
                <form method="POST" action="$_SERVER['PHP_SELF']">
                    <p>
                        <label for="email"> Your E-Mail Address: </label> <br/>
                        <input type="email" id="email" name="email" size="40" maxlength="150"/>
                    </p>

                    <fieldset>
                        <legend> Action: </legend>
                        <input type="radio" id="action_sub" name="action" value="sub" checked/>
                        <label for="action_sub"> Subscribe </label> <br>

                        <input type="radio" id="action_unsub" name="action" value="unsub"/>
                        <label for="action_unsub"> Unsubscribe </label
                    </fieldset>

                    <button type="submit" name="btnSubmit" value="Send"> Submit </button>
                </form>
    EOB; //closing delimiter must start with no spaces nor tabs infront of it. 
        }

        echo $display_block;
    ?>

My questions are;

  1. In this case, heredoc acts exactly as $display_block = " string inside double quotes " ? So that should I use { } as action="{$_SERVER['PHP_SELF']} in order to wrap array item in double quotes?

But when I use that, I get a new error;

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\PHP\2000.php on line 59

Line 59 is: ?>

  1. Why cannot I use like this for the action attribute? action = “<?php echo $_SERVER[‘PHP_SELF’]; ?>”
  • 写回答

1条回答 默认 最新

  • duanjuan1103 2016-05-28 16:04
    关注

    Because; you have spaces before your EOB; (closing identifier) being 4 of them; remove them.

    As per the manual on heredoc:

    Warning

    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 on UNIX systems, including Mac OS X. The closing delimiter 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.

    Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

    Example #1 Invalid example

    <?php
    class foo {
        public $bar = <<<EOT
    bar
        EOT;
    }
    ?>
    

    You also didn't close off </label => </label>.

    And action="$_SERVER['PHP_SELF']" needs to read as action="<?php $_SERVER['PHP_SELF']; ?>" which is equivalent to just doing action="" which is what you need to use here to make it work, action="".

    You can use a bracing method action="{$_SERVER['PHP_SELF']}" if you really want/must use that syntax.

    "Why cannot I use like this for the action attribute? action = “<?php echo $_SERVER[‘PHP_SELF’]; ?>”"

    If those are the actual quotes used being curly quotes “ ”, then those need to be changed to regular double quotes " and would contribute to parse errors.

    However, the <?php echo and ;?> would have needed to be removed.


    Footnotes:

    I noticed <input type="radio" id="action_sub" name="action" value="sub" checked/> and this tells me you are using your script as a subscription site.

    Having a radio button set to default as "Yes, subscribe me", is illegal in many countries, as it is in mine being Canada.

    The Canadian Anti-Spam Legislation prohibits this being the default (already selected) and the user him/herself needs to select it, not you for them.

    • You should be made aware of that and there are heavy fines/consequences for this type of action.

    Either uncheck it by default, or use only a single checkbox that is unchecked (have them check it) and using a conditional statement against it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog