Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    May 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Coding Style

    I am learning to use PHP, and I have two related questions. First off, I made a page, that based on the pass word entered will direct the user to the corresponding site. When I do this, is it better to end the PHP after the if, elseif or else statements and have the page in regular html, or should I keep the the PHP script and have it just echo the html? For example:
    Code:
    <?php if (x == 'y') :?>
    // HTML code
    <?php endif; ?>
    or
    Code:
    <?php if (x == 'y') {
    echo('//All HTML code');
    }
    ?>
    I am used to writing C++/Java, so the second option comes almost by habit to me, but how much harder is that on the server? Also, I was wondering if there are coding standards for PHP, like W3C for HTML. Any help would be greatly appreciated. Thanks!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've always heard not to switch to much in and out PHP mode, cause that would slow down the processing, so the second option should be faster ...

  • #3
    New to the CF scene
    Join Date
    Jul 2003
    Location
    Not for you to know.
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Faster, yes, but harder to code. You'd have to either use single quotes or put a backslash in front of all your quotes (and dollar signs), which is a pain.

    And, as far as I know, there aren't any official coding standards, though there are quite a few unofficial ones.

    ^_^

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would say that using the alternative if-construct syntax is uncommon, you can do the same with curly braces as well.

    About switching between PHP and HTML mode... when I started learning PHP, it was very common and everyone build apps with this technique. However I abandoned that style quite soon because IMO it leads to an obfuscated code flow. Especially if the HTML parts are very long and also contain other, nested PHP mode parts, such code can easily grow into spaghetti code and be hard to maintain in the long-term perspective.

    I favor much more keeping the HTML in strings and having one point where they are actually printed, so you can easily process the resulting output before it gets sent to the browser, in order to plug in new functionality.
    De gustibus non est disputandum.

  • #5
    New Coder
    Join Date
    May 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the replies. From what I can gather, I guess it really comes down to what I am comfortable doing. I guess there is a plus and minus to everything huh? Thanks again.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just to throw a spanner in the works, i use a combination of both.

    <?php if($whatever) {?>
    <html> etc
    <?}?>

    I dont like echoing out all the HTML code for the reasons mentioned previously, it needs an awful lot of escaping, plus it messes up the syntax highlighting in UltraEdit.

    As far as standards go, if it works and iyou can read it, and it isn't really slow - stick with it. Under normal circumstances, only you will see your coding.
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by mordred
    I would say that using the alternative if-construct syntax is uncommon, you can do the same with curly braces as well.

    About switching between PHP and HTML mode... when I started learning PHP, it was very common and everyone build apps with this technique. However I abandoned that style quite soon because IMO it leads to an obfuscated code flow. Especially if the HTML parts are very long and also contain other, nested PHP mode parts, such code can easily grow into spaghetti code and be hard to maintain in the long-term perspective.

    I favor much more keeping the HTML in strings and having one point where they are actually printed, so you can easily process the resulting output before it gets sent to the browser, in order to plug in new functionality.
    Do you really mean that you keep all your html in string vars?! It's very hard and I think it itself makes the code really ugly, that spaghetti that you mentioned! I don't do it myself and I pretty much use pure HTML, but I think the best choice for doing that if you don't wana clutter your php code with big string vars is to use ob_start, but then it's hard too! So, what's wrong with using html? You can put all your output area in a function/class and then do whatever you wana do in there.

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I realize I wasn't very clear with my description. I meant that I find reading code that constanly switches between PHP and HTML mode in the same script tedious and hard to follow. Like this:

    PHP Code:
    if ($a > 0) { ?> 
        <table>
        <tr> <?php
        
    if ($a 5) {
            print 
    strlen($foobar); 
        }
        else { 
    ?>
            nothing stands here
        <?php
        
    }
    }
    Bad example, I know that. When i said "keeping in strings" I thought also of constructs like this:

    PHP Code:
    $myObj->setContent(sprinf(file_get_contents('header.tpl'), $param1$param2));
    print 
    $myObj->getContent(); 
    In header.tpl all my HTML for the header is contained - and some %s markers that get subsituted by the values of $param1 and $param2. A minimal templating if you so want. Alternatively, use php echo()s and output buffering and store the result in a string, as you said.
    Most of the time you only use PHP to write small portions of HTML code, like

    $header = '<strong>' . strtoupper($authorName) . '<strong>';
    print $header;

    I don't see how that's actually harder than dropping out of and later into PHP code again. Concerning the double quote issue, most IDEs should be able to handle this by their syntax-coloring scheme.
    De gustibus non est disputandum.

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,051
    Thanks
    10
    Thanked 94 Times in 92 Posts
    Jumping 'in and out' of PHP tags carries no overhead , that said I agree with Mordred that eventually this can lead to some very hard to read or more important hard to update code.

    but it is no slower , in fact its probably faster in most cases.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Most of the time you only use PHP to write small portions of HTML code
    Yes, that's right, but when you're programming for yourself! I really liked that idea of having tpl files containing your html. And also having wild cards later replaced by real values. But what do you do with for loops?! It seems that you forgot the graphic designers! Whenever it's a table that should be outputted to the browser, we have to use some for loops (most of the times nested foreaches ) and then for each table we have a different template. For such a thing you can not use a tpl file with some wild cards, because you don't know the number of loops. So, you have to code it manually. And then for each installation of your script, there is a different customer with a different graphic designer who says you should change the whole cellpadding, border styles, colors and ..., what do you do with that?! If you say that you write the script then pass it on to your graphic designers, then it's a different story.
    Last edited by ConfusedOfLife; 07-15-2003 at 02:27 PM.

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ConfusedOfLife, I only stated my personal preference for dealing with PHP and HTML in one script. It's not a one-size-fits-it-all solution, it was only a reply to the original posters question, and that was primarily a question about common coding style.

    Concerning the for-loop question... that opens up a new can full of worms. There is no ultimate solution to the problem of graphic designers and programmers working together on the same sources. What your questions hints at is the use of templates. And the whole template debate is very controversial, and there do exist many different approaches for establishing a working template system. Smarty, Flexy, raw PHP, XSLT, etc. pp.
    If you want to discuss the different merits of template engines, please open a new thread.

    Just so much: In the end it's always the programmer who writes the for-loop. The graphic designer creates a layout of what it should look like, and the programmers turn it into PHP code or whatever. At the company I work at, we don't let the designers write loop constructs, they screw it up everytime.
    De gustibus non est disputandum.

  • #12
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just so much: In the end it's always the programmer who writes the for-loop. The graphic designer creates a layout of what it should look like, and the programmers turn it into PHP code or whatever. At the company I work at, we don't let the designers write loop constructs, they screw it up everytime.
    Lol! Then you're right! They always do it, but let's be fair, sometimes they can really make it!

    ConfusedOfLife, I only stated my personal preference for dealing with PHP and HTML in one script.
    Well, I wanted your personal idea dear! You know, based on my experience (!!) I know that you always have some cool things in your mind, and whenever possible, I just try to get some things outta your mind. I really like your approaches
    Last edited by ConfusedOfLife; 07-15-2003 at 08:12 PM.


  •  

    Posting Permissions

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