使用PHP连接HTML的良好编程实践
我正在尝试将
PHP与HTML连接起来,我想知道它的最佳实践是什么.到目前为止,我已经接近它了: – 已经完全显示了脚本 –
<?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 FilesInstallShield$isVersionSystem' (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 FilesInstallShield$isVersionSystemTSconfig.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,please follow the steps below:<br /><br /> 1. Launch a Command Prompt window and browse to the directory - 'C:Program FilesInstallShield$isVersionSystem' (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 FilesInstallShield$isVersionSystemTSconfig.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回应所有内容,而不是像上面那样做? 当我看到人们制作的大型项目时,我注意到每个文件中几乎没有任何东西,而且肯定不会有很多HTML可见.假设大多数都在对象或类中,我是否正确?任何建议或参考将不胜感激.提前致谢. 解决方法
您可能会从不同的人那里得到不同的“最佳实践”描述.我会使用
PSR-1 代码格式标准,其中包括重点:
>文件应该声明符号(类,函数,常量等)或引起副作用(例如生成输出,更改.ini设置等),但不应该同时执行这两种操作. 我还建议您的PHP代码不要直接打印任何HTML,而是使用模板.我是模板引擎的忠实粉丝,其中有很多.人们也会说“php本身就是一个模板引擎”,如果你愿意的话你也可以使用它.保持模板(显示)尽可能多的逻辑. 提前与您的团队确定您的标准并坚持下去.关于是否回声或?>的观点可能存在差异.更好,但一旦你决定,保持一致.保持缩进也很重要.如果你这样做,你可能会找到使你的elseif不正确的缺失括号. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |