For a beginner of programing, it's a good habit of writing codes in standard format. There are different rules in different companies/teams. Better than form your own, follow a standard of a popular application could be easier and more acceptable by people. For example, the WordPress Coding Standards and Inline Documentation.
Some good approach I just learned from it:
- When doing logical comparisons always put the variable on the right side.
I saw this in some codes, now I know why: If you forget an equal sign it'll throw a parse error instead of just evaluating true and executing the statement. It really takes no extra time to do, so if this saves one bug it's worth it.
- Use real tabs and not spaces, as this allows the most flexibility across clients.
In some courses, it's suggested that using spaces instead of tabs. Either of they has their reasons. As mentioned above, using the standards of popular applications could be better. And I have already had the habit of using tabs, so I just follow WordPress here.
- Tabs should be used at the beginning of the line and spaces should be used mid-line.
Exception for previous point: if you have a block of code that would be more readable if things aligned, use spaces.
- Ternary operators are fine, but always have them test if the statement is true, not false. Otherwise it just gets confusing.
- // GOOD example:
- // (if statement is true) ? (do this) : (if false, do this);
- $musictype = ( 'jazz' == $music ) ? 'cool' : 'blah';