How to name variables in an easy-to-understand manner
Sep 6, 2020
PHP
beginners
variables
programming study diary
#Programming study diary September 6, 2020 Summarize how to name variables that are indispensable for writing easy-to-understand code. Making variable names easy to read makes it easier to read and notice code mistakes.
How to write variables in #PHP
- -For PHP variables start with
$
- -After that, you can use half-width alphanumeric characters and underscores (
_
). (You can’t use a number immediately after $)
If you violate the above rules, an error will occur at runtime.
In addition, although it is not a rule (command rule), there are also the following rules.
- -Case sensitive.
- -Variable names can be capitalized, but variables with special meanings may be capitalized, so it’s best not to capitalize them.
It’s not a rule, but if it’s multiple words
Separate words with an underscore (_
) or capitalize the first letter after the second word.
☆ Point </ font>
When using multiple words, decide which one (whether to capitalize the beginning or use _
).
//bad example
$ userid = "123";
$ user_name = "Tanaka";
$ userGroup = "A";
// Good example
$ userID = "123";
$ userName = "Tanaka";
$ userGroup = "A";
#How to name variables What is the content of the variable stored? Show what the contents are. In other words, give the name a descriptive name for what the variable is used for. The variable name should be just the right length, not too long and not too short. It will be easier to understand if the table name and column name are given as they are to the data acquired from the database table.
//bad example
$ data // I don't know what the data is
$ a // I don't know what I'm storing
$ var // I don't know what variable
// Good example
$ error_message // The error message is stored
$ user_id // Contains the user's id