PHP Variable naming recommendations

Source: Internet
Author: User
Keywords PHP Variable naming recommendations

PHP is a weak type of language, if there are many variables in the program, plus PHP Many of the functions of the name are very chaotic, chaotic, and see the dazzling.

Unified coding style, even variable naming, is very important in team development.

I have been engaged in the development of PHP for many years, in order to make the team more effective, and gradually formed a PHP naming habit (after a friend said that there is already a Hungarian nomenclature), so that their procedures look very clear.

Below I will give you experience, not necessarily apply to others, but still share.

String, strings, preceded by a variable str
Copy PHP content to clipboard
PHP Code:
The following variable is a string type
$strMessage = ' Hello world! ';


arrays, array type, plus a in front of variables, one-dimensional arrays using nouns singular, multidimensional arrays using word plural
Copy PHP content to clipboard
PHP Code:
The following variable is a one-dimensional array
$aData = Array (1, 2, 3, 4, 5, 6);

The following variable is a multidimensional array
$aMembers = array (' id ' = = 123456, ' username ' = ' ABC ', ' email ' = ' abc#abc.com ');


integer, int variable, preceded by ' n '
Copy PHP content to clipboard
PHP Code:
The following variable is an integer
$nCount = $pBS->member->getcount ();


Boolean, Boolean preceded by ' B '
Copy PHP content to clipboard
PHP Code:
The following variable is a Boolean type
$bEncode = true;


float, floating point, plus ' F ' in front
Copy PHP content to clipboard
PHP Code:
The following variable is a floating-point type
$fSave = 0.8; 80 percent


Pointer types, such as classes. Add ' P ' to the front
Copy PHP content to clipboard
PHP Code:
The following is an instantiation of a class
$pBP = new Bluepage;


Resource, resource type, plus ' RS ' in front
Copy PHP content to clipboard
PHP Code:
$rsConn = mysql_connect (' localhost ', ' user ', ' pw ');

$rsHandle = fopen ($strFilename);


Unspecified variables, using MX
Copy PHP content to clipboard
PHP Code:
$mxData = GetData ();


Custom functions, starting with fn_
Copy PHP content to clipboard
PHP Code:
function Fn_halterror ($strErrorMessage)
{
Do sth ...
}


A comprehensive example (using a paging Class):
Copy PHP content to clipboard
PHP Code:
Include ("lib/bluepage.class.php");
$pBP = new Bluepage;

$rsConn = mysql_connect (' localhost ', ' root ', ' 123456 ') or Die (Mysql_error ());
mysql_select_db (' Test ', $rsConn);

$strQuery = "Select COUNT (' ID ') from test";
$nCount = $pBP->mygetcount ($strQuery, $rsConn); Total achieved
if ($nCount < 1)
{
Fn_halterror ($aMessages [' nodata ']);
}
$nShowNum = 10;

Pagination Group and HTML
$aPDatas = $pBP->get ($nCount, $nShowNum);
$strHtml = $pBP->gethtml ($aPDatas);

The paging data contains offset, which takes the data
$strQuery = "SELECT * FROM Test LIMIT". $aPDatas [' offset ']. ", " . $nShowNum;
$rsResult = mysql_query ($strQuery);

  • Related Article

    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.