Quicky 2: PHP Technique, Type Prefixing Variable Names

Target Audience:

Anyone using the PHP programming language.  Anyone wondering more about Naming Styles specifically dealing with variable type prefixing.

Languages Used:

PHP 5+

Related Material:
Quicky 1 – PHP is Loosely Typed – What Does That Mean?
PHP Good Practices 1 – Naming Conventions
PHP Good Practices 2 – Self-Documenting Code

Introduction:

Have you ever seen code where the author prefixes their variable names using type prefixes?  You know, when every variable includes the data type at the beginning of the name such as str or int?  This is called Type Prefixing and can although it may appear silly and pointless at first, it has some very useful purposes.

PHP is loosely typed! If you don’t know what this means, please read about it here…

Because PHP is loosely typed, you can assign any kind of data you want to any variable in php.  Keeping track of your variables and scope can become challenging, however it is very important.  When you start crossing over data types in variables implicitly, unexpected results can occur, and often do.

So if you are like ma and want a way to organize and keep track of data types within your code, using type prefixes works great.  By using this simple technique I am able to quickly tell what data type I intended for a certain variable simply by looking at it.  Even though the PHP Naming Conventions do not include type prefixing, they also don’t say you shouldn’t! It only takes an extra second and will save you head aches, and time later.

Type Prefixing is Easy!

To type  prefix your variable all you have to do is start the name off with the appropriate prefix, following the case style as determined by your adopted naming conventions.  The  Zend naming conventions for php state that variables should use cameCase, where the first letter of each word except the first is capitalized.  Using this convention to type prefix a string variable all I would do is this.

$strName

Or if I wanted to type prefix an integer variable I would do this.

$intAge

Now when I see these variables in my code I will understand quickly what data type I had intended for that variable.  This will help me keep my data consistent and avoid some unexpected run time errors later.

Type Prefixing for IDE’s ‘Code Complete’

If you use and IDE such as Zend Studio, Dreamweaver, or another you probably have some kind of  ‘code complete’ feature built into it.  This is where when you start typing a variable you have already declared and the IDE brings up a list of variables you in your app that are similar to what you are typing.  You can then choose your variable from that list and the IDE will automatically place it in the code.

By type prefixing your variable names you can very quickly locate variables by simply starting to type in their data types.  As you type $int, a list of all your integer variables will be displayed for you to choose from.  This is a lot easier then trying to keep track of all your variable names in your head!

Common Type Prefix  Abbreviations:

Data type Prefix Example
Boolean bln blnFound
Byte byt bytRasterData
Date (Time) dtm dtmStart
Double dbl dblTolerance
Integer int intQuantity
Long lng lngDistance
Object obj objCurrent
String str strFName

Wrap It Up

Remember, type prefixing is not a required practice, and it won’t change how your code executes.  It will however help you organize your variables and speed up your production when working inside an IDE that has a ‘Code Complete’ feature.  A little extra typing can actually save you a lot of time.

— Live, Learn, Share —

Helpful Books:

Advertisements