Variable naming rules in the PHP Getting Started Tutorial. When learning development, we need to know some common sense. for example, variable naming rules are a very important habit, if you develop a good naming convention for variables, the teams you work with will learn more about development, and we need to know some common sense. for example, variable naming rules are a very important habit, if you develop a good naming convention for variables, the teams you work with will be more likely to accept you.
1. class name
(1) use the camel naming method (this is common in almost all languages)
(2) Try to have less than 3 words
(3) do not use uppercase letters for all acronyms. Eg: use GetHtmlStatic instead of GetHTMLStatic.
2. name functions and methods
Generally, each method and function executes an action, so the naming of them should clearly describe their purpose.
Eg: use CheckForErrors () instead of ErrorCheck (), and use DumpDataToFile instead of DataFile ().
3. class attribute naming
The attribute name should be prefixed with the character "m. The prefix "m" is followed by the same class naming rules.
Eg: var mVar;
4. variable naming
Makes all variables look different in the code and easy to recognize.
Local variable naming: all letters use lowercase letters and use "_" as the boundary of each word.
Eg: $ time_of_error
Global variable name: with the prefix "g"
Eg: global $ gLog;
Static variable name: with the prefix "s"
Eg: static $ msStatics = 0;
Reference variable name: with the prefix "r"
Eg: var mrStatus;
String, string type, add str before the variable
The code is as follows: |
|
// The following variable is of the string type. $ StrMessage = Hello World! ; Array: array type. add a to the variable. one-dimensional arrays use singular nouns and multidimensional arrays use the plural words. // The following variable is a one-dimensional array. $ AData = array (1, 2, 3, 4, 5, 6 ); // The following variable is a multi-dimensional array. $ AMembers = array (id => 123456, username => ABC, emai L => abc # abc.com ); Integer, an integer variable, plus n // The following variable is an integer. $ NCount = $ pBS-> Member-> getCount (); Boolean: The boolean type is preceded by B. // The following variable is Boolean. $ BEncode = true; Float, float type, plus f
// The following variable is floating point $ FSave = 0.8; // off Pointer type, such as class. Add p
// The following is the instantiation of a class $ PBP = new BluePage; Resource, resource type. add rs to the front
$ RsConn = mysql_connect (localhost, user, pw ); $ RsHandle = fopen ($ strFilename ); |
Unknown variable, using mx
The code is as follows: |
|
$ MxData = getData (); |
User-defined function, starting with fn _
The code is as follows: |
|
Function fn_HaltError ($ strErrorMessage) { // Do something... }
|
A comprehensive example (using paging classes ):
The code is as follows: |
|
Include ("lib/BluePage. class. php "); $ PBP = new BluePage; $ RsConn = mysql_connect (localhost, root, 123456) or d Ie (mysql_error ()); Mysql_select_db (test, $ rsConn ); $ StrQuery = "select count ('id') FROM test "; $ NCount = $ pBP-> myGetCount ($ strQuery, $ rsConn); // obtain the total number If ($ nCount <1) { Fn_HaltError ($ aMessages [nodata]); } $ NShowNum = 10; // Pagination array and html $ APDatas = $ pBP-> get ($ nCount, $ nShowNum ); $ StrHtml = $ pBP-> getHTML ($ aPDatas ); // The page data contains offset, and the data is retrieved. $ StrQuery = "SELECT * FROM test LIMIT". $ aPDatas [offse T]. ",". $ nShowNum; $ RsResult = mysql_query ($ strQuery ); |
...