I am sure this is a very newbish question that has been asked hundreds of times before, and probably there isn't a "right" answer. I've searched several sites and forums, and the answers left me even more confused!

I would like to start as I mean to go on - with a uniform coding structure that is going to be easy to read and use! So, when should I be using smallStartCamelCaps, when should I use AllCamelCaps, and when should I be using underlined_words?

At the moment, I tend to define variables as $myVariable and HTML form stuff as input name="InputBox". I think maybe this is force of habit from VB.NET - is it a bad practice?

The same goes for tables in MySQL. I read contradicting things about these. Generally, I have a tables called ID (caps), and the other fields I try to keep to one word, all lower case. Is this OK? I'm sure I've read somewhere that you should try to keep it all lower case. Should I put id instead of ID? And what about field names, is this the same? I've seen people use FieldName, fieldName, fieldname, field_name or even something like item_fieldName (the last one seems kinda pointless to me).

On another note, what is the difference between:

"
'
`

When should each one be used?

Again, I've seen people using a combination, sometimes even in the same code, e.g.:

$row["name"]
$row['name']

SELECT item1,item2 FROM mytable
SELECT `item1`,`item2` FROM `mytable`

    So, when should I be using smallStartCamelCaps, when should I use AllCamelCaps, and when should I be using underlined_words?

    Decide for yourself and be consistent. If you are working on existing code, follow the existing conventions.

    At the moment, I tend to define variables as $myVariable and HTML form stuff as input name="InputBox". I think maybe this is force of habit from VB.NET - is it a bad practice?

    It is acceptable.

    Generally, I have a tables called ID (caps), and the other fields I try to keep to one word, all lower case. Is this OK?

    Yes. However...

    I'm sure I've read somewhere that you should try to keep it all lower case.

    The convention is to write the keywords in SQL statements in upper case. With such a convention, lower case identifier names stand out.

    Should I put id instead of ID?

    Since you are keeping the other column names lower case, you might as well use id instead of ID.

    And what about field names, is this the same? I've seen people use FieldName, fieldName, fieldname, field_name or even something like item_fieldName (the last one seems kinda pointless to me).

    Up to you, but again, be consistent. I would match the naming convention for column names with the naming convention for variable names since they tend to correspond. Likewise, I would match the naming convention for table names with the naming convention for class names since they tend to correspond.

    On another note, what is the difference between:

    "
    '
    `

    When should each one be used?

    In the context of SQL, double quotes (") are used to quote identifier names (e.g., column and table names), single quotes (') are used to quote strings, and backticks (`) are used to quote identifier names in MySQL only (unless you enable support for double quotes to quote identifier names).

      By no means are any of these hard-and-fast "rules", but general practice seems to be, when using "camel case", to capitalize the first word when referring to a class name, and lower case when referring to a variable or function, e.g.:

      echo SomeClass::$someStaticProperty;
      $example = new Example();
      $example->anyMethod();
      

      As far as ID fields in database tables, I prefer to not simply call them "ID" or "id", as you then have the same name in virtually every table. I find it's easier to immediately see what my queries are doing by calling each such column by combining the table name with "_id", e.g. "user_id" for the "user" table. For some reason I prefer all lower-case with underscores instead of camel case for table and column names, reserving all upper-case for SQL keywords and functions.

      SELECT user_id, first_name, last_name FROM user WHERE last_name LIKE 'A%';
      

        By no means are any of these hard-and-fast "rules", but general practice seems to be, when using "camel case", to capitalize the first word when referring to a class name, and lower case when referring to a variable or function

        I prefer to use camel case for class and variable names, with class names capitalised, and then name my variables in lower case with words separated by underscores.

        As far as ID fields in database tables, I prefer to not simply call them "ID" or "id", as you then have the same name in virtually every table. I find it's easier to immediately see what my queries are doing by calling each such column by combining the table name with "_id", e.g. "user_id" for the "user" table.

        I prefer just using id, with the reasoning that the table name can be used as the prefix.

        For some reason I prefer all lower-case with underscores instead of camel case for table and column names, reserving all upper-case for SQL keywords and functions.

        As I recommended, I match my table/column naming convention with class/variable naming convention. Consequently, my table names are capitalised, with the column names in lower case with words separated by underscores. I find that an advantage of this naming convention, besides consistency and identification, is that if I find myself unable to come up with a good name for a join table, I can simply use the names of the tables involved and separate them with underscores.

          4 days later

          Thanks! Those answers help a lot 🙂

            Write a Reply...