Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > DEVELOP ROCK SOLID CODE IN PHP: PART 2


Sponsors





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

Variable variable names

Now for some magic. Newer developers think using variable variables is a convoluted way to do a task and often shun it. Actually, they are fairly easy to understand and use. Variable variables have helped me out of tight situations more than once and are an important language element. In fact, there are situations where you can't escape using variable variables. I'll examine one such real-life situation soon, but first let's see what variable variables really are. Start by trying out the code in Listing 4:

Listing 4. Code with variable variables
First, the code in Listing 4 declares a variable named $myStr and assigns the string I to it. The next statement defines another variable. But this time the name of the variable is the data in $myStr. $$myStr is a way of instructing PHP to make another variable saying, "I want a variable with the name you'll find in the variable $myStr. Of course, $myStr must be defined for doing this. So now, you have a variable named I and have assigned it the string am. The next statement does the same thing and puts the string great in a variable named am. And am is nothing but the data in the variable I

That clears things, but what about those strange curly braces in the echo statements? That's how you print variable variables. If you omit the braces, PHP attaches the dollar sign ($) to the contents of the variable while printing. The braces tell PHP to evaluate the things inside them first.

Try thinking this way: what do variables do? Simply, they abstract or represent data that can change with a name that remains the same. Variable variables do the same thing; they abstract data. But in this case, the data they contain is actually the name of another variable.

The example I gave in Listing 4 was to familiarize you with variable variables. The actual power of variable variable names comes from the fact that you can make variable variable names dynamically at run-time. you'll exploit this feature while constructing a configuration file parser.

Configuration file parser

In my experience, users often complain when editing configuration files in PHP while configuring applications to run. The configuration is nothing but a list of global variables that other scripts in the application depend on to find files, URLs, and other settings that control the behavior of the application. Often a missing dollar sign, semicolon or an unclosed comment breaks the entire code. What could help the user?

Suppose you give the user a file to edit with simple name-value pairs separated by an equals sign. The configuration file look something like Listing 5. Text on lines that begin with # is treated as a comment.

Listing 5. Sample configuration file
Makes sense? Yes, it does...why make the user edit the configuration when you can parse files in PHP? In fact, it's highly desirable. Remember your application must hide all complexity from the user while still letting him know that he's the one in control.

You'll make a function that does the parsing, so you use it without modifications wherever you want. Let's split the task into simpler steps:

  1. Read the file line by line
  2. Throw everything after hash characters on a line
  3. Separate the line into two strings throwing the equal sign away
  4. Remove extra spaces that occur in the strings
  5. Declare variables accordingly

To write the last step, nothing but variable variables will do. Listing 6 shows the code:

Listing 6. Parsing function
Regular expressions remove the hashes. Although the expressions here are simple, be aware that complex regular expressions waste a lot of CPU time. Also, it is a bad decision to parse a configuration file again and again for every page. A better option is to store the parsed output as a PHP script using variables or define statements. I prefer defines using the define() function because values once set cannot be altered at run-time. You'll find an implementation that you can modify according to your needs in Resources.



View Develop Rock Solid Code In PHP: Part 2 Discussion

Page:  1 2 3 4 Next Page: Summary and Resources

First published by IBM developerWorks


Copyright 2004-2017 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.