TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 09-06-2007, 11:26 PM   #1 (permalink)
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
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:31 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design