View Full Version : Where should I write my code?
heavensgate15
05-29-2009, 06:21 AM
Hello, I'm a beginner in writing php code... I wondered where should I write my code in a script... is it above the html code, center, or below the html code....
<? php
// code here
?>
<html>
// code here
</html>
OR
<html>
<?php //code here ?>
</html>
OR
<html>
// code here
</html>
<?php
//code here
?>
What is the right way? is there any disadvantage if I write it above my html script? or in middle of my html script? or after my html script?:confused:
forum_amnesiac
05-29-2009, 08:49 AM
I personally always put the PHP code at the top of the page, it executes first so that is where I believe it should be.
Wherever you put it in your code it will execute first, except under certain circumstances where you can stop it fully executing.
So for me it comes down to a matter of personal choice
Schmoopy
05-29-2009, 11:50 AM
Well it really depends what you're doing, I like to group together my PHP code with relevant parts in the HTML, for example if I have a form that executes some PHP code on the same page I will have the PHP just after the form HTML so that I know what it relates to.
In some cases, you have to put PHP code within the HTML when you echo something for example, for it to be in the correct div.
If you had:
<html>
<div id="menu">
<?php getMenuItems(); ?>
</div>
</html>
You'd have to keep that specific code there, because if you moved it to a different location, the menu items would end up in a different part of the document.
Like if you tried to put that code in here:
<html>
<head>
<title><?php getMenuItems(); ?></title>
</head>
</html>
This should be fairly obvious, but it's going to mess up your page.
There is no disadvantage to putting all your PHP code at the top, bottom or interlaced within the HTML, your script won't run any faster or slower. For readability however, I think it's best if you group PHP and HTML together where they're relevant.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.