PHP Style of Writing Codes

Discussion in 'PHP' started by aisyaziz, Mar 26, 2012.

  1. #1
    Hi everyone,

    So, I'm new in PHP. I know that PHP is basically inline codes. However, from what I've seen, (and from experiences with ASP classic), sometimes coders would rather write all the functions before any of the html elements. And in the inline codes, they'll call the functions/variables.

    e.g.

    <?php $var = 'test'
    .... codes codes functions functions...
    ?>
    <div>
    <?php echo $var ?>
    </div>

    or..

    <div>
    <?php echo 'test' ?>
    </div>

    which one is the better style?
     
    aisyaziz, Mar 26, 2012 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Ideally you should completely (or as much as possible) separate logic from the html/output portion of the coding. This provides much better organization. It may not necessarily be as efficient, but efficiency comes in at a distant second on a large, complex, project where organization is absolutely essential. MVC is a popular method of segmenting different parts of code.
     
    jestep, Mar 26, 2012 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    Just to build on what jestep posted, you'll almost always have to nest PHP code with HTML code (unless you use a template engine like Smarty which, for over 99% of projects I would personally argue against). However, like jestep stated, good practice involves separating logic as much as you can from your output). For example, code that queries a database for data, and transforms that data in to some meaningful form of information should ideally be in a separate file to your HTML code. It's generally good practice for your HTML file to take that information, iterate through and present it to the user.

    You can imagine how hard it would be for yourself, let alone other programmers who come on to a project, to sift through a file thousands of lines long with PHP and HTML code randomly all over the place.
     
    Alex Roxon, Mar 26, 2012 IP
  4. aisyaziz

    aisyaziz Guest

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yea, that makes sense! thanks!

    the first language i learned was asp classic. and the codes i looked at were in between the html tags. i found it easy to understand but a little too messy - though it did become very confusing when it comes to large junk of codes. thanks a lot!
     
    aisyaziz, Mar 26, 2012 IP
  5. zero_ZX

    zero_ZX Member

    Messages:
    83
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    I also recommend MVC, however mixing everything is possible. It's just a lot of hassle, and difficult to see through as well as understand.

    In the start I wrote php mixed with html, cause I didn't know better though. If you like to avoid MVC for any reason, then It's highly recommended that you keep your functions at the top. This will help you get a rid of many header errors..
     
    zero_ZX, Mar 30, 2012 IP
  6. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #6
    I would say none!

    If you would display data you can use <?=$var;?>
    And if you are using variables in stings use " otherwise use '

    so $text = "hello $world";
    or $text = 'Hello '. $world;

    :)

    And if you want to do more with MVC please start just by learning the language proper! the idea behind MVC is to split code and design but for a beginner, don't even start minding that... just make your scripts and enjoy them! :)
     
    EricBruggema, Apr 1, 2012 IP
  7. abyssal

    abyssal Guest

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you're looking for something to organize your work, then you can try NetBeans IDE, a software developed by Oracle which helps you organize your code.
     
    abyssal, Apr 1, 2012 IP
  8. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #8
    You should not be using short tags anymore.

    And I disagree. A firm understanding of the theory behind programming (as in the philosophy behind MVC), even for a beginner will teach you to write clean, efficient code. Why even bother wasting time programming poorly and learning bad habits?
     
    Alex Roxon, Apr 1, 2012 IP
  9. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #9
    I am agree with you.
     
    webshore88, Apr 1, 2012 IP
  10. JellyBean Fanatic

    JellyBean Fanatic Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Learn MVC mate. Once you get the hang of it, it'll greatly reduce the time you take to build sites. Especially when your database is wrapped in an ORM.
    To answer your question though, your functions and logic should go at the top before the <html> tag. This allows you to do error checking and redirects before any headers are sent. (you cant redirect after output is started to the browser).
     
    JellyBean Fanatic, Apr 1, 2012 IP