PHP variable naming suggestions

Source: Internet
Author: User

PHP is a weak language. If there are many variables in the program, and many functions in PHP are named in disorder, they will be dazzled.

Uniform coding style and even variable naming are very important in team development.

I have been engaged in PHP development for many years. In order to make the team more efficient, I gradually formed a naming convention for PHP (some friends later said it was an existing Hungarian naming convention ), make your program look very clear.

Next I will share my experience with you. It may not be applicable to others, but I will share it with you.

String, string type, add str before the variable
Copy the PHP content to the clipboard.
PHP code:
// 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.
Copy the PHP content to the clipboard.
PHP code:
// 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, email => abc # abc.com );


Integer, an integer variable, plus n
Copy the PHP content to the clipboard.
PHP code:
// The following variable is an integer.
$ NCount = $ pBS-> Member-> getCount ();


Boolean: The boolean type is preceded by B.
Copy the PHP content to the clipboard.
PHP code:
// The following variable is boolean.
$ BEncode = true;


Float, float type, plus f
Copy the PHP content to the clipboard.
PHP code:
// The following variable is floating point
$ FSave = 0.8; // off


Pointer type, such as class. Add p
Copy the PHP content to the clipboard.
PHP code:
// The following is the instantiation of A Class
$ PBP = new BluePage;


Resource, resource type. Add rs to the front
Copy the PHP content to the clipboard.
PHP code:
$ RsConn = mysql_connect (localhost, user, pw );

$ RsHandle = fopen ($ strFilename );


Unknown variable, using mx
Copy the PHP content to the clipboard.
PHP code:
$ MxData = getData ();


User-Defined Function, starting with fn _
Copy the PHP content to the clipboard.
PHP code:
Function fn_HaltError ($ strErrorMessage)
{
// Do something...
}


A comprehensive example (using paging classes ):
Copy the PHP content to the clipboard.
PHP code:
Include ("lib/BluePage. class. php ");
$ PBP = new BluePage;

$ RsConn = maid (localhost, root, 123456) or die (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 [offset]. ",". $ nShowNum;
$ RsResult = mysql_query ($ strQuery );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.