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:

4 thoughts on “Quicky 2: PHP Technique, Type Prefixing Variable Names

  1. This is called Hungarian notation in case you didn’t know. It’s a practice that is not used as much as it once was. See http://ootips.org/hungarian-notation.html for example, and the Wikipedia entry on Hungarian notation.

    As the ootips article mentions a big disadvantage of Hungarian notation is that when the type of a variable changes from, say, integer to long, all references to the variable must be changed.

    In a loosely-typed language such as PHP that doesn’t even have different types for byte, integer, and long this kind of naming convention doesn’t make a lot of sense. Giving up the flexibility of loose typing and automatic type conversion in order to conform to an 80s-era programming convention is not a good trade-off. And IDE auto-complete is more useful if the variables are named by function or context and not by type.

    1. Won’t disagree with you on any of that. Good Feedback. The only comment I would make is that this practice is still highly used in corporate America for many reasons. Personally, I prefer not to prefix my variables for many reasons, some you mentioned. However, I do try to keep my data typed, especially when dealing with OOP, even in a loose typed language such as PHP. With PHP you have the choice to enforce type yourself, or not at all. I use to think the same way you do until I started to see the benefits of strictly typed variables. But I do also remember the flexibility with auto casting like PHP does. Why not use the best of both worlds?

  2. I came to PHP (and Python) after 20 years of C and C++ and other strongly-typed languages. I find loose typing convenient, and I rarely run into type mismatch problems in PHP. Reducing the scope of variables (encapsulation) has a bigger payoff in my opinion than strong typing or variable naming conventions.

  3. I don’t use this kind of practise for the same reason of Greg but also because it adds constraints to use.
    Just use simple var name with good name not in camel case, like it writen in the official PHP naming convention.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s