Home > Backend Development > PHP Tutorial > PHP introductory tutorial variable naming rules_PHP tutorial

PHP introductory tutorial variable naming rules_PHP tutorial

WBOY
Release: 2016-07-13 10:49:37
Original
1045 people have browsed it

When learning development, we need to know some common sense. For example, variable naming rules are a very important habit. If you develop good variable naming habits, the team you work with will be more likely to accept you in the future.

1. Class naming

(1) Use camel nomenclature (this is common to almost all languages)

(2) Try to be less than 3 words

(3) Do not use all capital letters for abbreviations. eg: Use GetHtmlStatic instead of GetHTMLStatic.

2. Function and method naming

Usually each method and function performs an action, so their naming should clearly state their purpose.

eg: Use CheckForErrors() instead of ErrorCheck(), and use DumpDataToFile instead of DataFile().

3. Class attribute naming

Attribute names should be prefixed with the character "m". The prefix "m" is followed by a consistent rule for class naming.

eg: var mVar;

4. Variable naming

Makes all variables look different in the code and easy to identify.

Local variable naming: all letters are lowercase, and "_" is used as a delimiter for each word

eg: $time_of_error

Global variable naming: with prefix "g"

eg: global $gLog;

Static variable naming: with prefix "s"

eg: static $msStatics = 0;

Reference variable naming: with prefix "r"

eg: var mrStatus;


string, string type, add str

in front of the variable
The code is as follows Copy code
 代码如下 复制代码

//下面一个变量为字串型

 $strMessage = Hello World! ;
array,数组型, 在变量前面加a, 一维数组使用名词单数,多维数组使用词复数

//下面一个变量为一维数组
$aData = array ( 1 , 2 , 3 , 4 , 5 ,6) ;
//下面一个变量为多维数组
$aMembers = array ( id => 123456 , username => ABC , emai
l => abc#abc.com ) ;
integer,整数型变量,在前面加上n

 //下面一个变量为整数
$nCount = $pBS->Member->getCount() ;
boolean,布尔型在前面加上b


 //下面一个变量为布尔型
$bEncode = true ;
float,浮点型, 在前面加上f


 //下面一个变量为浮点型
$fSave = 0.8 ; // 8折
指针类型,比如类。在前面加上p


 //下面是一个类的实例化
$pBP = new BluePage ;
resource,资源型,在前面加上rs

$rsConn = mysql_connect ( localhost , user , pw ) ;
$rsHandle = fopen( $strFilename );

//The next variable is of string type

$strMessage = Hello World! ;

Array, array type, add a in front of the variable, use the singular noun for a one-dimensional array, and use the plural noun for a multi-dimensional array

 代码如下 复制代码

$mxData = getData() ;

//The next variable is a one-dimensional array

$aData = array (1, 2, 3, 4, 5,6);

//The next variable is a multi-dimensional array
 代码如下 复制代码
function fn_HaltError ( $strErrorMessage )
{
    // do sth...
}
$aMembers = array ( id => 123456 , username => ABC , emai l => abc#abc.com ) ; integer, integer variable, add n in front //The next variable is an integer $nCount = $pBS->Member->getCount() ; boolean, Boolean type is preceded by b //The next variable is of Boolean type $bEncode = true; float, floating point type, add f in front //The next variable is of floating point type $fSave = 0.8; // 20% off Pointer types, such as classes. Add p in front //The following is an instantiation of a class $pBP = new BluePage; resource, resource type, add rs in front $rsConn = mysql_connect ( localhost , user , pw ) ; $rsHandle = fopen( $strFilename );
Unspecified variables, use mx
The code is as follows Copy code
$mxData = getData() ;
Custom function, start with fn_
The code is as follows Copy code
function fn_HaltError ( $strErrorMessage ) { // do sth... }

A comprehensive example (using paging class):

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 ) ; //取得总数
if ( $nCount < 1 )
{
fn_HaltError( $aMessages[nodata] ) ;
}
$nShowNum = 10 ;

//分页数组与html
$aPDatas = $pBP->get( $nCount , $nShowNum ) ;
$strHtml   = $pBP->getHTML( $aPDatas ) ;

// 分页数据里包含有offset,取数据
$strQuery  = "SELECT * FROM test LIMIT " . $aPDatas[offse
t] . ", " . $nShowNum ;
$rsResult  = mysql_query( $strQuery );

Copy code
include ( "lib/BluePage.class.php" ) ;
$pBP = new BluePage ;

$rsConn = mysql_connect( localhost , root , 123456 ) or d
ie( mysql_error() ) ;
mysql_select_db( test , $rsConn );

//Paging array and html
$aPDatas = $pBP->get( $nCount , $nShowNum ) ;
$strHtml = $pBP->getHTML( $aPDatas ) ;
//Paging data contains offset, get data
$strQuery = "SELECT * FROM test LIMIT " . $aPDatas[offse
t] . ", " . $nShowNum ;
$rsResult = mysql_query( $strQuery );
http://www.bkjia.com/PHPjc/632701.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632701.htmlTechArticleWhen learning development, we need to know some common sense. For example, variable naming rules are a very important habit. If you develop good variable naming habits, the team you work with will be more...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template