johnsmith153 Posted April 22, 2009 Share Posted April 22, 2009 People talk about standards compliant - mainly CSS / HTML. But how is the best way to lay out php code? I.e the standards compliant way. I have been doing PHP for a year or so, but have only self-taught myself. I feel I know more advanced stuff and not the basics. This is how I would do it now: <?php //document code as much as possible $variableName=2; //perform if statement if($variableName==1) {echo "Yes";} else { if(1==2) { echo "ok"; } else { echo "no"; } } ?> Does the layout of my code look like it was produced by a novice? Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/ Share on other sites More sharing options...
Mchl Posted April 22, 2009 Share Posted April 22, 2009 Yes it does. Indentation helps a lot, so use it. There are many differnt conventions regarding coding style. It's usually what you prefer, or - if you work in a team - what the team assumed as a standard Here's example of what Zend Framework guidelines look like http://framework.zend.com/wiki/display/ZFDEV/PHP+Coding+Standard+(draft) Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816618 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 You're going to get a lot of mixed answers - here's my translation: <?php //document code as much as possible $variableName=2; //perform if statement if ($variableName==1) { echo 'Yes'; } else { if (1==2) { echo 'ok'; } else { echo 'no'; } } ?> Truth is, there's no right and wrong way - it ALL depends on YOU. The layout you feel comfortable with. How you label your variables. How you name your files. How you structure each script as well as in how you break your HTML and PHP as I've seen some people have one massive PHP script that uses echo to throw HTML to the browser - I think that's messy so I start and close PHP tags throughout scripts. When using a text editor that color codes everything it's soooo much easier to read the file. Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816619 Share on other sites More sharing options...
Maq Posted April 22, 2009 Share Posted April 22, 2009 Does the layout of my code look like it was produced by a novice? Yes, you have no indentation. It's hard to debug that way especially with a lot of code. I tend to use Java's standard of formatting because that's what I learned first and use at work. A lot of different frameworks, applications, and third party systems have their own standards. i.e. Drupal - Coding standards. Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816621 Share on other sites More sharing options...
kenrbnsn Posted April 22, 2009 Share Posted April 22, 2009 If you don't indent properly, you are going to run into problems. Using PHP Formatter, gives: <?php //document code as much as possible $variableName = 2; //perform if statement if ($variableName == 1) { echo "Yes"; } else { if (1 == 2) { echo "ok"; } else { echo "no"; } } ?> See how much easier it is to see the flow? Ken Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816623 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 Classes must be named by following the naming conventions. The brace is always written on the line underneath the class name ("one true brace" form). Every class must have a documentation block that conforms to the phpDocumentor standard. Any code within a class must be indented the standard indent of four spaces. Only one class is permitted per PHP file. What crap! There's absolutely NO way I'm going to go for their course now. Thanks Mchl - that just made my mind up for me. Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816627 Share on other sites More sharing options...
Mchl Posted April 22, 2009 Share Posted April 22, 2009 These are standards for Zend Framework contributors. Not guidelines for general PHP coding. Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816637 Share on other sites More sharing options...
Zhadus Posted April 22, 2009 Share Posted April 22, 2009 Not to completely go with the flow or anything, but first off I agree that identation definitely makes the difference. Additionally I'd recommend and there are some examples in this post, for things like conditionals, I believe it is MUCH easier to read and understand if you put spaces with the operators. <?php if ($variableName==1) { ?> Versus <?php if ($variableName == 1) { ?> When you have multiple lines of code, that bit of separation makes it easier on the eyes to identify problems, particularly if your editor doesn't syntax highlight. Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816638 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 I know - that's why I'm not going to go for their course. I was thinking about it at work but now my mind is made up. Back on topic (lol) Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816639 Share on other sites More sharing options...
Maq Posted April 22, 2009 Share Posted April 22, 2009 The best thing to do is use an IDE. That way you can import standard formatting files and use a hot-key to format it automatically for you (I think in eclipse it's, ctr+shft+f). Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816643 Share on other sites More sharing options...
johnsmith153 Posted April 22, 2009 Author Share Posted April 22, 2009 Woooah... So many answers. The easier the question.. the more answers you tend to get. I once wrote a really lengthy, difficult question - would take 10 minutes just to read - and nobody replied! Thanks for all the responses. kenrbnsn's example is how I tend to remember most professionally created code looking like. Just another thing - I have seen this used, what do people think of this: instead of <?php //various code etc $v=1; if(1==1) { echo "<a href='link.html'>Link</a>"; } //more code $a=1; ?> they have used... <?php //various code etc $v=1; if(1==1) { ?> <a href='link.html'>Link</a> <?php } //more code $a=1; ?> Is that ok. Although it tends to be when displaying a lot of html, not just one line. Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816650 Share on other sites More sharing options...
Maq Posted April 22, 2009 Share Posted April 22, 2009 The easier the question.. the more answers you tend to get. Makes sense you get a larger range of skill. Is that ok. Although it tends to be when displaying a lot of html, not just one line. No, like every suggested, you need indentation! Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816655 Share on other sites More sharing options...
johnsmith153 Posted April 23, 2009 Author Share Posted April 23, 2009 I wasn't asking about the layout on my last question, more as to if this is acceptable: <?php if(1==1) { ?> hello there, should I do like this, or must I use echo inside php tags <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-816988 Share on other sites More sharing options...
Maq Posted April 23, 2009 Share Posted April 23, 2009 Oh, sorry. Sure you can do that, there are multiple ways to display raw HTML. 1) Break out of PHP. 2) Use HEREDOC. 3) Echo it out. 4) There are probably more but I can't think of any. Quote Link to comment https://forums.phpfreaks.com/topic/155225-easy-question-how-is-the-correct-way-to-lay-out-php-code/#findComment-817030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.