Good programming practice with PHP with HTML concatenation

I am trying to concatenate PHP with HTML, and I was wondering what is best for it. So far I have approached him like this: -EDATED 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, how PHP and HTML come together when and when it is required. I am wondering, however, if this is actually the best approach, and should I use PHP to repeat everything, and not do it as shown above?
I am also trying to write an elseif block below this, which gives me an error that says that elseif is unexpected, and that is why I think I'm not mistaken about it.

When I look at the huge projects that people have done, I notice that there is almost nothing in every file, and, of course, not a lot of HTML code. Do I believe that most of them relate to objects or classes? Any suggestions or links will be appreciated. Thanks in advance.

+4
source share
2 answers

You will probably get a different description of โ€œbest practicesโ€ from different people. I would go with the PSR-1 code formatting standards, which includes an important point:

  • Files MUST either declare characters (classes, functions, constants, etc.) or cause side effects (for example, generate output, change .ini settings, etc.), but DO NOT do both.

I would also recommend that your php code doesn't print any HTML directly, but use templates instead. I am a big fan of engine templates, and there are many of them. People will also say that "php itself is a template engine" and you could use it for that if you want. Store as much logic as possible from the template (display).

Define your standards in advance with your team and stick to them. There may be a difference of opinion as to whether echo or ?> Is better, but as soon as you decide, be consistent. It is also important to keep the indentation. If you do this, you can probably find the missing bracket that makes your elseif wrong.

+5
source

As batmegakapa has already explained, doing logic / generating and viewing content are two different tasks. The complexity of modern web applications often requires such an approach in order to preserve readable and understandable code, and possibly also to be reused.

This is (approximately) the reason why the MVC model (model / view / controller) and template systems today are components of most frameworks.

Of course, it is impossible to give one solution, because it depends on personal preferences and requirements that make this issue controversial, too localized and do not customize Q & A-style.

However, following the rule, so as not to mix logic and content, you will most likely do it like this (basic aproach):

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) ?> 

One more thing, by the way: I had doubts about what part of the code you provided really belongs to the if block, because it was not obvious to me. The HTML code for your example somehow doesn't make much sense.

This may be just for demonstration, but it took a few seconds to really understand the meaning of your example in my head. This should be more readable - especially in large projects.

+2
source

Source: https://habr.com/ru/post/1480002/


All Articles