What's your style?
I finished my C class (yay) and I'm seriously going to learn PHP now, really, I am. In a book I'm reading the author formats conditionals like
<?php
if (condition) {
// do something
} elseif (condition2) {
// do something
} else {
// do something else
}
?>
I'm pretty sure there is no officially proper way to format your code, but that looks really cool compared to the way I did it in C
<?php
if(condition)
{
// do something
}
elseif (condition2)
{
// do something
}
else
{
// do something else
}
?>
How do you format your code? And besides conditions, how else do you do it? Like constants in all upper case, vars in all lower case with underscores, capital first letter of a function etc. Do you know of any website that has a pretty sweet guide to formating code in PHP?
baldrick posted this at 22:06 — 23rd March 2007.
He has: 388 posts
Joined: Apr 2005
Here is a nice guide on it and if you want to cheat here is a program that does it for you
timjpriebe posted this at 14:04 — 26th March 2007.
He has: 2,667 posts
Joined: Dec 2004
I format mine more like that second example. My personal opinion is that it's just a preference issue, though.
As far as variables, I tend to do the lowercaseUppercase type of formatting. I always start with lowercase, then any new word starts with an uppercase letter. I avoid underscores in general. Again, just personal preference.
Tim
http://www.tandswebdesign.com
andy206uk posted this at 14:30 — 26th March 2007.
He has: 1,758 posts
Joined: Jul 2002
I prefer the first example. There's no right or wrong, it's a personal preference thing, just don't forget to indent anything inside the curly braces otherwise your code will be a nightmare to read.
I keep vars all in lower case and don't use any underscores in variable names.
functions I keep in all lower case but with underscores separating words (not sure why!), but i prefer to stay consistent with my existing style.
Andy
Abhishek Reddy posted this at 16:56 — 26th March 2007.
He has: 3,348 posts
Joined: Jul 2001
In the absence of any other motivation for a given style, you might like to follow the PHP PEAR coding standards. That's about as official as it gets.
rubensson posted this at 20:28 — 26th March 2007.
They have: 2 posts
Joined: Mar 2007
I agree with the subjectiveness of the issue. I surprised myself with using the first variant the other day because it was more readable when embedded in HTML. In Java (which I program for a living) I always put curly brace on new lines. I think it is important to use the same style within a project if there are several developers. The code should look the same in all files within the same source tree.
timjpriebe posted this at 13:09 — 27th March 2007.
He has: 2,667 posts
Joined: Dec 2004
Hmm, rubensson brings up a good point. I forget that when embedding PHP in HTML, I do occasionally use the first variant, with little or no ill effects. ie It doesn't bother me personally to stray from my norm.
Tim
http://www.tandswebdesign.com
a_gajic posted this at 13:55 — 29th March 2007.
They have: 71 posts
Joined: Aug 2001
When I taught myself PHP I used your first method. Then, at Uni we learnt Java and they taught it the other way.. which I then preferred and now use!
Adam
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.