【问题标题】:Good Programming Practices With PHP With Concatenating HTML使用连接 HTML 的 PHP 的良好编程实践
【发布时间】:2013-05-09 22:13:33
【问题描述】:

我正在尝试将 PHP 与 HTML 连接起来,我想知道它的最佳实践是什么。到目前为止,我是这样处理的:--EDITED TO DISPLAY FULL SCRIPT--

<?php include("header.php"); ?>


<div id="main">
    <table id="mainTable">
        <tr>
            <td id="leftPane">
                <div class="pgbody">

                    <div class="subblock">
                        <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
                        <table width="100%" cellpadding="10" cellspacing="0">
                                <tr>
                                    <td colspan="3" style="width:99%">
                                    <label for="isVersion">InstallShield Version</label><br />
                                    <select name="isVersion" onchange="javascript:setTimeout('__doPostBack(\'ddlVersion\',\'\')', 0)" id="isVersion" class="box">
                                        <option value="2012Spring">2012 Spring</option>
                                        <option value="2012">2012</option>
                                        <option value="2011">2011</option>
                                        <option value="2010">2010</option>
                                        <option value="2009">2009</option>
                                        <option value="2009 Express">2009 Express</option>
                                        <option value="IS2008">2008</option>
                                        <option value="IS2008 Express">2008 Express</option>
                                    </select>

                                    <tr>
                                        <td colspan="3" style="width:99%">
                                            <br />
                                            <input type="checkbox" name="no_internet" value="no_internet"> no_internet
                                        </td>
                                    </tr>

                                    <tr>
                                        <td colspan="3" style="width:99%">
                                        <br />
                                        <input type="submit" name="submit" value="Submit">
                                        </td>
                                    </tr>
                                </tr></table>
                        <?php

                        if(isset($_POST['submit']))
                        {
                            echo "<h2>Response</h2><br />";
                            $isVersion = $_POST["isVersion"];
                            $output_script = "
                                    <p>Hello,  <br />
                                    To activate InstallShield, please follow the steps below:<br /><br />

                                    1. Launch a Command Prompt window and browse to the directory - 'C:\Program Files\InstallShield\\$isVersion\System' (or 'Program Files (x86)' on a 64 bit machine)<br />
                                    2. You will need to pass the parameter '/return' to the executable 'TSconfig' as below<br />
                                    'C:\Program Files\InstallShield\\$isVersion\System\TSconfig.exe /return'<br />
                                    3. Providing the machine has a valid internet connection the license will deactivate and the message in the dialog will reflect this<br />
                                    4. Re-launch InstallShield.exe and you will be presented with the same activation dialog as before<br />
                                    5. Proceed with the activation normally<br />
                                    6. The dialog should show a successful activation message and the product should remain activated at this stage.<br />
                                     </p>";
                        ?>

                        <div class="ResponseBox" style="background-position: 0 0;">
                            <div class="ResponseText">
                                <?php echo $output_script;
                        }
                                 ?>
                        <?php
                        elseif(isset($_POST['submit']) && $_POST['no_internet'])
                        {
                            echo "<h2>Response</h2><br />";
                            $isVersion = $_POST["isVersion"];
                            $no_internet = $_POST["no_internet"];
                            $output_script = "
                                    <p>Hello,  <br />
                                    To activate InstallShield, please follow the steps below:<br /><br />

                                    1. Launch a Command Prompt window and browse to the directory - 'C:\Program Files\InstallShield\\$isVersion\System' (or 'Program Files (x86)' on a 64 bit machine)<br />
                                    2. You will need to pass the parameter '/return' to the executable 'TSconfig' as below<br />
                                    'C:\Program Files\InstallShield\\$isVersion\System\TSconfig.exe /return /$no_internet'<br />
                                    3. Providing the machine has a valid internet connection the license will deactivate and the message in the dialog will reflect this<br />
                                    4. Re-launch InstallShield.exe and you will be presented with the same activation dialog as before<br />
                                    5. Proceed with the activation normally<br />
                                    6. The dialog should show a successful activation message and the product should remain activated at this stage.<br />
                                     </p>";
                        ?>
                        <div class="ResponseBox" style="background-position: 0 0;">
                            <div class="ResponseText">
                                <?php echo $output_script;
                        }
                                 ?>
                            </div>
                        </div>    
                    </div>
            </td>

            <td id="rightPane">
                <div class="PromoBox" style="background-position: 0 0;">
                    <div class="PromoText">
                            <?php
                            echo "<h2>Related KB Article: </h2><br />";
                            echo "<h3>Deactivation of IS - Q201081</h3>";
                            ?>
                    </div>
                </div>
            </td>
                                </tr>
        </tr>
    </table>
</div>

如您所见,PHP 和 HTML 在需要时相互连接。我想知道,但是,如果这实际上是最好的方法,我应该使用 PHP 来回显所有内容而不是像上面那样做吗?
我还试图在下面写一个 elseif 块,这给了我一个错误,指出 elseif 是意外的,这就是让我认为我正在接近这整个事情错误的原因。

当我查看人们制作的大型项目时,我注意到每个文件中几乎没有任何内容,而且肯定没有很多 HTML 是可见的。我假设大部分内容都在对象或类中是否正确?任何建议或参考将不胜感激。提前致谢。

【问题讨论】:

  • 这样的方法既干净又整洁,我会说保持这样
  • 在我看来,永远不要echo HTML。在您的模板文件中,您应该主要包含 HTML,并在正确的位置打印一些 PHP 变量。 PHP 是一种模板语言,所有控制结构都有一个模板版本(例如if () : ... endif;),如果在服务器中打开它们,您甚至可以使用速记 PHP 块(如&lt;?=$x?&gt; 打印$x 的值) .顺便说一句,我投了赞成票,这个问题太宽泛了,会引发辩论。
  • 这里我认为 PHP 是一种服务器端语言,有很多可用的模板引擎,而速记标签即将被弃用?
  • 如果您不想以意大利面条式的代码结尾,那么将 PHP 逻辑与 HTML 混合不是一个好习惯。您应该在 HTML 中使用 PHP 来打印动态值和控制结构:php.net/manual/en/control-structures.alternative-syntax.php.
  • @adeneo 嗯,我不知道。 5.4 &lt;?= 中的 AFAIK 甚至在短标签关闭的情况下也可以工作,似乎鼓励这样做。我也看不出有理由弃用控制结构的模板版本。我想我已经读过一些关于 ASP 样式和脚本 PHP 标记被弃用的内容,但从未使用过这些……

标签: php html


【解决方案1】:

您可能会从不同的人那里得到对“最佳做法”的不同描述。我会使用PSR-1 代码格式标准,其中包括重点:

  • 文件应声明符号(类、函数、常量等)或引起副作用(例如生成输出、更改 .ini 设置等),但不应同时声明两者。

我还建议您的 php 代码根本不要直接打印出任何 HTML,而是使用模板。我是模板引擎的忠实粉丝,那里有很多。人们还会说“php 本身就是一个模板引擎”,如果您愿意,可以使用它。尽可能多地保留模板(显示)之外的逻辑。

提前与您的团队确定您的标准并坚持下去。对于echo?&gt; 是否更好,可能存在意见分歧,但一旦您决定,请保持一致。保持缩进一致也很重要。如果这样做,您可能会找到导致elseif 不正确的大括号。

【讨论】:

    【解决方案2】:

    就像 batmegakapa 已经解释的那样,执行逻辑/内容生成查看是两个不同的任务。现代 Web 应用程序的复杂性通常要求采用这种方法来保持代码的可读性和可理解性,以及可重用性。

    这就是(大致)原因,为什么 MVC 模式(模型/视图/控制器) 和模板系统是当今大多数框架的组件。

    当然,不可能给出一个解决方案,因为这取决于个人喜好和要求——这使得这个问题有争议,过于本地化,不适合问答风格。

    但是,当遵循不混合逻辑和内容的规则时,您很可能会这样做(基本方法):

    no_internet_form.php:

    <?php
    if(isset($_POST['submit']) && $_POST['no_internet'])
    {
        //echo "<h2>Response</h2><br />";
        $Version = $_POST["isVersion"];
        $no_internet = $_POST["no_internet"];
        $script_result = execute_the_incredibly long_script($Version, $no_internet);
    }
    
    // more business logic ...
    
    include 'views/no_internet_form.html.php';
    ?>
    

    views/no_internet_form.html.php:

    <?php if(isset($script_result)): ?>
    <div class="ResponseBox" style="background-position: 0 0;">
        <div class="ResponseText">
            <?php echo $script_result; ?>
        </div>
    </div>
    <?php endif; //isset($script_result) ?>
    

    顺便说一句:我怀疑您提供的代码的哪一部分实际上属于if 块,因为这对我来说并不明显。您的示例的 HTML 代码不知何故没有多大意义。

    这可能只是为了演示,但我花了几秒钟才真正理解你的例子的含义。这应该更具可读性——尤其是在大型项目中。

    【讨论】:

    • @Explosion-Pills 感谢您的代码编辑。我现在太习惯使用 Twig 模板引擎了。一旦普通的 PHP“模板”不再满足一个人的需求,绝对值得一看。
    • @Explosion-Pills 好的,我会发布原件。我已经用完整的脚本编辑了原始帖子。你能弄清楚为什么我不能使用 elseif 语句,因为它一直说这是意料之外的。
    • @DaveMelia 如果if 块和elseif 标记之间可能没有语句。关闭和打开 PHP 标记确实算作这样的“声明”,并且是不允许的。
    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 2011-07-29
    相关资源
    最近更新 更多