Results 1 to 3 of 3

Thread: Where should I write my code?

  1. #1
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Question Where should I write my code?

    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?

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    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

  3. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    heavensgate15 (06-01-2009)

  4. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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:
    PHP Code:
    <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:

    PHP Code:
    <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.

  5. The Following User Says Thank You to Schmoopy For This Useful Post:

    heavensgate15 (06-01-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •