
- Drop me a line! Contact me here.
PHP References
PHP References
PHP has numerous ways of accessing information within a script. All these methods require a 'handler', a variable that contains a pointer to what you're accessing. It could be a simple variable, an array, an object, a database connection, or an image in GD, all of them require a handle that you use to access them. PHP handles are defined using a dollar symbol.
These handles are simply called variables in basic PHP.
One of PHPs more advanced features is the availability of references. A reference is a variable variable. What a reference allows you to do is access a variable using the contents of another variable.
The resulting code will print "hello world". The PHP script has substituted $bar for "foo", so the code works like $"foo", which in the example is equal to "hello world". Of course, it is possible to nest references as many times as you wish, although more than 3 or 4 levels does tend to get rather confusing and should be avoided if at all possible.
Of course, finding a use for references might not be immediately obvious. They are terrifically powerful though, and well worth getting to grips with.
A very simple example.
Imagine a form that requires multiple text input boxes. There are many possible ways to draw the inputs, iterating through a series of records from a database resultset for example, but to keep things easy I shall use a very simple for..next loop.
Once you have your input boxes named input_1 through input_5 you will then need to access them when they're returned by the user. This is achieved by a block of code similar to the code that was used to draw them.
This is a rather trivial use of references, but it serves to demonstrate one of their possibilities. They are much more useful in scripts that access database connections and objects based on variable criteria, but thats a more complex subject and not really very helpful in a short article.
<?php
$int = 3;
$string = "This is a string";
$database = mysql_connect("localhost","root","password");
$object = new directory();
?>
These handles are simply called variables in basic PHP.
One of PHPs more advanced features is the availability of references. A reference is a variable variable. What a reference allows you to do is access a variable using the contents of another variable.
<?php
$foo = "hello world";
$bar = "foo";
echo $$bar;
?>
The resulting code will print "hello world". The PHP script has substituted $bar for "foo", so the code works like $"foo", which in the example is equal to "hello world". Of course, it is possible to nest references as many times as you wish, although more than 3 or 4 levels does tend to get rather confusing and should be avoided if at all possible.
<?php
$foo = "hello world";
$bar = "foo";
$car = "bar";
$dar = "car";
echo $$$$dar;
?>
Of course, finding a use for references might not be immediately obvious. They are terrifically powerful though, and well worth getting to grips with.
A very simple example.
Imagine a form that requires multiple text input boxes. There are many possible ways to draw the inputs, iterating through a series of records from a database resultset for example, but to keep things easy I shall use a very simple for..next loop.
<?php
for ($x=1;$x<=5;$x++) { echo "<input type=\"text\" name=\"input_$x\"><br>"; }
?>
Once you have your input boxes named input_1 through input_5 you will then need to access them when they're returned by the user. This is achieved by a block of code similar to the code that was used to draw them.
<?php
for ($x=1;$x<=5;$x++) {
echo $${"input_".$x};
}
?>
This is a rather trivial use of references, but it serves to demonstrate one of their possibilities. They are much more useful in scripts that access database connections and objects based on variable criteria, but thats a more complex subject and not really very helpful in a short article.
Comments
Comments are not currently being accepted for this article.
Sidebar
- © Ooer.com Chris Neale 2007
- PHPGD.com
- Powered by PHP
- Database by MySQL
- DB Queries: 7
- DB Time: 0.0519 seconds
