Skip to content
//-->

June 17, 2010

Variable Naming in PHP

PHP is a loosely typed language, meaning any variable can be any type.  You could be using your variable as an integer one minute and then declare it as an array the next.

There are long-term repercussions  for avoiding to take care of your code.  Say you return to a project a year later to fix a bug.  You’re not going to have the crystal-clear understanding you had when writing it.  You’ll see all these variables and not know what is what until you go and debug your whole script to figure out what to change.  Variable naming schemes won’t eliminate this problem, but it’s going to save the first step by identifying what variables are which type.

I use the xyVariable method, which involves using characters, x, to denote the scope of the variable, and y, denoting the type.  The different type and scope representations can be as thorough as you want them to be.

For scope, I use these:

  • Global variables – g
  • Local variables - l
  • Variables passed by parameter – p
  • Class member variables – c

Types of type:

  • Number – n
  • String – s
  • Boolean – l
  • Object – o
  • Array – a

You could obviously swap out the n type for i and d (integer and double) if you wanted to get more in-depth.  Here are some examples of variables you’d commonly see throughout my code:

1
2
3
4
5
6
7
$lcSQLQuery = "SELECT * FROM `table`";
$loSQLResult = mysql_query($lcQuery);
$lnNumRows = mysql_num_rows();
while ($laRow = mysql_fetch_assoc($loSQLResult))
{
    echo $laRow["data"];
}
Read more from Programming

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments