What's your style?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

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?

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 Smiling

timjpriebe's picture

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.

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's picture

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.

Smiling

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's picture

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.

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.