What Is the Best Practice to Use When Using PHP and HTML

What is the best practice to use when using PHP and HTML?

There are varying opinions on this. I think there are two good ways:

  • Use a templating engine like Smarty that completely separates code and presentation.

  • Use your second example, but when mixing PHP into HTML, only output variables. Do all the code logic in one block before outputting anything, or a separate file. Like so:

    <?php $content = doSomething();
    // complex calculations
    ?>
    <html>
    <body>
    <?php echo $content; ?>
    <div id="some_div">Content</div>
    </body>
    </html>

Most full-fledged application frameworks bring their own styles of doing this; in that case, it's usually best to follow the style provided.

What are the best practices for PHP syntax when used within HTML?

In PHP templates, that's exactly the way I do it, too (foreach/endforeach).

But really, when it comes to syntax, this is simply a matter of preference. Thus, no "best practice". Go with what you like and - more importantly - stick to it!

I'm sure there are good reasons for either approach.

PHP include best practices question

I include my views from my controllers. I also define file locations to make maintenance easier.

config.php

define('DIR_BASE',      dirname( dirname( __FILE__ ) ) . '/');
define('DIR_SYSTEM', DIR_BASE . 'system/');
define('DIR_VIEWS', DIR_SYSTEM . 'views/');
define('DIR_CTLS', DIR_SYSTEM . 'ctls/');
define('DIR_MDLS', DIR_SYSTEM . 'mdls/');
define('VIEW_HEADER', DIR_VIEWS . 'header.php');
define('VIEW_NAVIGATION', DIR_VIEWS . 'navigation.php');
define('VIEW_FOOTER', DIR_VIEWS . 'footer.php');

Now i have all the info i need just by including config.php.

controller.php

require( '../config.php' );
include( DIR_MDLS . 'model.php' );

$model = new model();
if ( $model->getStuff() ) {
$page_to_load = DIR_VIEWS . 'page.php';
}
else {
$page_to_load = DIR_VIEWS . 'otherpage.php';
}

include( VIEW_HEADER );
include( VIEW_NAVIGATION );
include( DIR_VIEWS . $page_to_load );
include( VIEW_FOOTER );

Good coding practice with PHP and HTML

It depends where and what kind of script you are using.Above from those two samples I will choose the first one.Because if you will go through the code you can see proper indentation with spaces are maintained there.It helps to debug the code.If you are asking about HTML codes then first work is to check whether the code is valid or not.Because code validation makes ease to debug. Here are some of the links, just go through them.Hopefully you will get some to make your code more good

PHP Coding style - Best practices

http://net.tutsplus.com/tutorials/php/30-php-best-practices-for-beginners/

http://www.ibm.com/developerworks/opensource/library/os-php-5goodhabits/index.html

http://framework.zend.com/manual/en/coding-standard.coding-style.html

Best practice for structuring HTML header in PHP

Not to troll, but you're describing the need for a PHP content management system, like WordPress or Drupal. That's exactly what they do.

There's no performance advantage to using PHP in the architecture you describe as opposed to creating separate HTML pages and including header and footer in them. Maybe it saves a few minutes over the course of a month, but you lose most of the automation features that make content so manageable. Plus CSS is a headache, and databases are more challenging. etc, etc.

PHP - HTML best practices to handle (submit) generated rows

You can use the following

<table id="YourTableId">
...
<tr data-id="yourrowId">
<td class="col1"> value1</td>
<td class="col2"> value2</td>
<td class="col3"> value3</td>
<td class="actions">
<a href="#"> Submit</a>
</td>
</tr>
....
</table>

your javascript code will be like

$(document).ready(function (){
$('#YourTableId a').off('click').on('click',function(e){
e.preventDefault();
var tr = $(this).closest('tr')
var data={ // here you can add as much as you want from variables
'id' : tr.data('id), // if you want to send id value
'col1': tr.find('.col1').text(),
'col2': tr.find('.col2').text(),
'col3': tr.find('.col3').text(),
};

$.ajax({
method: 'post',
url: 'your url goes here',
data: data,
success: function(result){
// handle the result here
}
});
});
});

Hope this will help you

Preferable ways of writing PHP code in a HTML page?

The first option it's 'better' (or I prefered this) for various reasons: because it's more readable, with code editors you could have syntaxis help and autocomplete and the page loads faster.

Another thing to consider is that you will be dealing with double and simple quotes in the second method that could cause errors:

echo "<div id='a'"+$var+"'...>";

But the 2 options are valid.

There are others questions like that: Outputting HTML with echo considered bad practice in PHP?



Related Topics



Leave a reply



Submit