View Single Post
Old 09-06-2007, 11:26 PM   #1 (permalink)
bluesaga
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default [Tip] Variable Prefixes

Something i learnt, fairly recently but has already been a huge timesaver, and keeps scripts to a strict standardised programming method is variable prefixes.

Variable prefixes are 1 to 2 characters prior to the variable name, in the variable, for example:
$szString instead of $string.

The following are prefixes i use:

m_ Member, this is used as a pre-prefix to show a member variable and is used like: $this->m_iInteger;
g_ Global, this is used as a pre-prefix to show a global variable and is used like: global $g_aGlobal;
a Array ($aArray)
sz String ($szString, $sz looks better than $s before you ask)
i Integer ($iInteger)
f Float ($fFloat)
b Boolean ($bBoolean)
p Object/Pointer ($pPointer)
m Mixed (i.e. could be any value - $mMixed)

Think of the following peices of code:
PHP Code:
<?php
$numbers 
= array(151015);
$count count($numbers);
$addition $numbers[1]+$numbers[2];
$division $numbers[1] / $numbers[2];
$integer $addition+$division;
?>
With the previous snippet of code, you may of noticed that i have thought the $integer would be an integer, but looking a little closer you would notice that division could cause non-integer numbers.

Now if you take the following peice of code:

PHP Code:
$aNumbers = array(151015);
$iCount count($aNumbers);
$iAddition $aNumbers[1]+$aNumbers[2];
$fDivision $aNumbers[1] / $aNumbers[2];
$iInteger $iAddition+$fDivision
Now it is made very clear that the $iInteger would not be an integer, due to the $fDivision clearly not being an integer, thus causing a float result which could cause problems within your application.


This is obviously a very simple example of the benefits of variable prefixes, but imagine using these within multiple thousand line applications, with variables being used across multiple includes, variable prefixes come in much more handy then! Other benefits include, the ability to type-cast/type-enforce and definately readability!
bluesaga is offline  
Reply With Quote