Developer Forums | About Us | Site Map


Useful Lists

Web Host
site hosted by netplex

Online Manuals

Develop Rock Solid Code In PHP: Part 2
By Amol Hatwar - 2004-01-05 Page:  1 2 3 4

Use variables effectively

The series Develop rock-solid code in PHP is about solving practical, real-life problems in medium- to large-scale applications. In this article, PHP veteran Amol Hatwar discusses how to use variables effectively. He also shows how to make script configuration easy by constructing a configuration file parser using variable variable names in PHP.

In my previous article, I looked at factors that must be considered during planning, designing, and even writing code. In this installment you will dive straight in, get your feet wet with code, and see things in action. If you haven't been through the previous article, this is a good time to read it.

Getting variables right
Variables and functions are essential features of any computer language. With variables, you can abstract data; with functions, you can abstract several lines of code. As Bruce Eckel puts it in his book Thinking in C++, all programming languages provide abstractions. Assembly language is a small abstraction of the underlying machine. Many so-called imperative languages that followed (such as Fortran, BASIC, and C) are abstractions of assembly language.

The complexity of the problems you are able to solve is directly related to the kind and quality of abstraction your programming language provides. Understanding how PHP handles variables and functions will help you use them effectively.

What's in a name?

As I mentioned in the previous article, naming and coding conventions are important. Whatever naming convention you use, remember to strictly adhere to it in a project. If you use the most widely-used naming conventions, your code will have a wider audience.

When naming variables, take special care when including scripts that you do not overwrite the variables you are using. This is a common source of bugs in large applications when new functionality is added. The best way to prevent this is to use prefixes. Use an abbreviation of the module in which the variable appears as a prefix. For example, if you have a variable that holds the user ID in a module that handles polls, you might name it as $poll_userID or $pollUserID.

View Develop Rock Solid Code In PHP: Part 2 Discussion

Page:  1 2 3 4 Next Page: Understanding variables in PHP

First published by IBM developerWorks

Copyright 2004-2024 All rights reserved.
Article copyright and all rights retained by the author.