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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了