NYCPHP Meetup

NYPHP.org

[nycphp-talk] Best practices for naming conventions & coding style?

lists at nopersonal.info lists at nopersonal.info
Mon Apr 27 16:51:45 EDT 2009


Hi everyone,

I've been making steady (if slow) progress with my understanding of
PHP/MySQL, but now that I'm finally starting to do more complex things,
I find that I really need to figure out a consistent naming convention &
coding style. I've read several articles on the subject and they all
seem to be different.

Is there a de facto professional standard, or is it just whatever you
personally prefer (provided that you're not part of a larger team with
specific guidelines)? So far I'm doing the following:

-Functions are all lower case, no numbers, underscores used to separate
words
-Variables (same as functions--should they be different?)
-Constants and MySQL reserved words & functions are all upper case
-Classes... I'm not that advanced yet!
-Plenty of comments to help me remember what the code was for
-Tabs for indentation
-No echoing HTML code except where moving between it and PHP is too messy
-Stick with single quotes for string literals where there are no
variable substitutions and/or where there ARE variable substitutions,
but where using double quotes would necessitate lots of escaping

Two things I've read about that I don't do are 1.) put spaces before &
after the string concatenator, and 2.) keep my opening/closing braces on
their own lines.


//I find it much easier to read:

if ($foo == '') {
    echo '<p id="message" class="error">'.$message.'</p>';
} else {
    echo '<p id="message" class="success">'.$message.'</p>';
}

//Than:

if ($foo == '')
{
    echo '<p id="message" class="error">' . $message . '</p>';
}
else
{
    echo '<p id="message" class="success">' . $message . '</p>';
}

Are any of the things I'm doing/not doing a major no-no? It's not too
late for me to unlearn bad habits, so I'd really appreciate any advice
you could give.

Thanks,

Bev







More information about the talk mailing list
Automatic Email Organization without missing anything!