PHP初学者:Html中的函数显示代码


beginner PHP: Html in function displaying code?

正在阅读这本书:

http://commons.oreilly.com/wiki/index.php/PHP_Cookbook/Forms

示例如下:

    <?php 
        if (isset($_POST['stage']) && ('process' == $_POST['stage'])) {
            process_form();
        } else {
            print_form();
        }
        function print_form() {
            echo <<<END
                <form action="$_SERVER[PHP_SELF]" method="post">
                What is your first name?
                <input type="text" name="first_name">
                <input type="hidden" name="stage" value="process">
                <input type="submit" value="Say Hello">
                </form>
        END;
        }
        function process_form() {
            echo 'Hello ' . $_POST['first_name'] . '!';
        } 
     ?>
然而

…当你加载页面时表单和按钮会显示出来但是它也会打印出

后面的所有代码

所以…加载的页面正确地显示了表单,但随后显示了文本:

END; }
            function process_form() {
                echo 'Hello ' . $_POST['first_name'] . '!';
            } 
         ?>

示例:

    $content = <<<EOL
        contentcontentcontentcontentcontentcontentcontentcontentcontent
EOL; // will end it
    $content = <<<EOL
        contentcontentcontentcontentcontentcontentcontentcontentcontent
    EOL; // will NOT end it

所以它需要在开始

EOF只有在结束标记前没有制表符或其他字符的行中才会关闭。

   <?php 
    if (isset($_POST['stage']) && ('process' == $_POST['stage'])) {
        process_form();
    } else {
        print_form();
    }
    function print_form() {
        echo <<<END
            <form action="$_SERVER[PHP_SELF]" method="post">
            What is your first name?
            <input type="text" name="first_name">
            <input type="hidden" name="stage" value="process">
            <input type="submit" value="Say Hello">
            </form>
END; // NOTE THIS, NO TABS BEFORE
    }
    function process_form() {
        echo 'Hello ' . $_POST['first_name'] . '!';
    } 
 ?>

如果我在你体内,我就会这么做:

        echo '<form action="$_SERVER[PHP_SELF]" method="post">
        What is your first name?
        <input type="text" name="first_name">
        <input type="hidden" name="stage" value="process">
        <input type="submit" value="Say Hello">
        </form>';

更整洁,更容易阅读。如果需要将所有代码放到另一个循环中怎么办?你需要缩进每一行,并确保在END之前不放制表符/空格;线。

如果另一个开发人员使用添加自动缩进的编辑器编辑该代码怎么办?: -)