开发者

where to place the php code?

开发者 https://www.devze.com 2023-01-16 19:34 出处:网络
i would like to know your point of view on where to position the PHP code on .php page and why? a) top of the document

i would like to know your point of view on where to position the PHP code on .php page and why?

a) top of the document

b) just above the html elements where i am going to use it.开发者_运维技巧

thank you.


c) In a different file and use a template engine such as smarty

http://www.smarty.net/

Your life will be beautiful and awesome after smarty.

EDIT : I won't downvote other solutions , but it's a very ugly anti-pattern to mix html code with php, you have good, stable and easy solutions to avoid that, use it now or your website will be a big mess of spaghetti code.


Depends on the purpose.

Database query related posts that determine the contents within the part, I call it before there is any input. Also any type of PHP commands that contain raw header information should be presented before any output is made.

Any content related stuff can be positioned anywhere on the page. PHP code is really everywhere - where ever, and however you want to create the HTML from your PHP dynamically.

My pages usually take this structure:

<?
include 'start.php';

$pagetitle = 'the services we offer (branding, web, print etc.)';
$metatitle = 'Our Creative Services (branding/logo, web, print)';

$scriptinclude = 'whatwedo.js';

include 'header.php'; // contains the <body><head></head><body> and a few more elements to start the header/menubar etc. 
?>
<div class="full_grid" id="index_slide">
// content here, mixed with PHP if you like... 
</div>

<?
include 'footer.php'; // contains the footer HTML, as well as </body></html> etc. to wrap things up. 
?>


I put as much code as I can at the top. And only use php withi HTML where I need loops or output data.

This gives me a better overview of the code and it's easier to work with.


Keep your code and HTML as separate as possible. Have them in entirely separate files where you can.

Your HTML should be as much pure HTML as possible, and your PHP code should contain as little HTML as possible.

Obviously, you're producing a web page, so there will have to be some mixing, but keep it as limited as possible: The only code you should mix in with your HTML should be the one-liners to place specific bits of PHP-generated code into your HTML template.


It completely depends what you're doing with it. Personal preference for me is to create any functions I need at the top and then scatter inline php throughout the document calling the functions at the top of the page.

If something needs calculating and it can be done at the top, it's much easier to read and debug if you keep it all in one place. And keeping this the same throughout all your files will help too. What you could do is just include a config file at the top of the page with any site-wide functions you need too, so you don't have to copy and paste through all your files.


If you are only using one PHP file then definitely put all PHP code at the top, then the HTML below with variables where necessary.

For example, $title = 'Page title'; at the top of the page, then <h1><?=$title?></h1> in the HTML portion of the page.

However a better solution is to have two (or more) files. The main one contains all the PHP logic to grab/process data, while the second one is a "view" file containing mostly HTML. The simply include your view file from the main PHP file.

0

精彩评论

暂无评论...
验证码 换一张
取 消