Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good Programming Practices With PHP With Concatenating HTML

Tags:

html

php

I'm trying to concatenate PHP with HTML and I was wondering what the best practice for it is. So far, I have approached it like so: --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>

As you can see the PHP and HTML concatenate one another as and when it is required. I am wondering, however if this is actually the best approach and should I use PHP to echo everything rather than doing it like above?
I am also trying to write an elseif block below this which gives me an error which states the elseif is unexpected, which is what makes me think that I'm approaching this whole thing wrong.

When I look at huge projects that people have made, I've noticed that barely anything is within each file, and most certainly not a lot of HTML is visible. Am I correct in assuming that most of it goes within objects or classes? Any suggestions or references will be greatly appreciated. Thanks in advance.

like image 712
Dave Melia Avatar asked Mar 25 '23 00:03

Dave Melia


1 Answers

You'll probably get a different description of "best practices" from different people. I would go with the PSR-1 code formatting standards, which includes the important point:

  • Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.

I would also recommend that your php code not print out any HTML directly at all but use templates instead. I'm a big fan of templating engines, and there are a ton of them out there. People will also say that "php itself is a templating engine," and you could use it for that if you wanted. Keep as much logic out of the template (display) as possible.

Decide on your standards ahead of time with your team and stick with them. There may be a variance in opinion about whether echo or ?> is better, but once you have decided, be consistent. It's important to keep indentation consistent too. If you do that, you'll probably be able to find the missing brace that makes your elseif incorrect.

like image 148
Explosion Pills Avatar answered Apr 08 '23 06:04

Explosion Pills