Apr 24 2008
Some More PHP Abilities You May Not Know About
Because my first PHP abilities you may not know about post seemed to be useful, here is part 2: some more PHP abilities you may not know about. If you want to learn about ternary operators, calling functions from strings, or variable variables, check out the previous post. Read on to lean about some other things that you can do with PHP.
Modify variable by reference
<?php $test = 'new'; modify($test); echo $test; //prints new variable; function modify(&$variable){ $variable .= ' variable'; } ?>
By using the "&" symbol before the parameter in the function, you pass a reference of the variable to the function.
This saves you from having to say $david = duplicate($david), which can be nice in some situations.
Point a variable to another
This is similar to the example above. You are able to set a variable to reference another by using "=&", an example below:
<?php $david = 'i am david'; $joe =& $david; $joe = 'i am joe'; echo $david; //prints i am joe. ?>
In this example, any change to $joe will result in a change to $david, because $joe is essentially $david.
Default parameter value
I'm amazed at how many people don't know about this. When setting up a function's parameters you are able to specify a default value if the parameter is not passed.
<?php func('test'); function func($foo, $bar = 'value'){ echo $foo . $bar; //we didnt pass $bar, so the result will be 'testvalue' } ?>
The parameter $bar is not passed to the function, so the default value of 'value' is used instead.
Create variable from a string
I explained variable variables in the PHP post previous to this one, but did you know that you can combine two (or more) strings then treat the result as a variable?
<?php $var = 'my'; $bar = 'var'; $myvar = 'testing'; echo ${$var . $bar}; //prints the $myvar variable (testing) ?>
Man, PHP is crazy.
Like this post? Subscribe to the full RSS feed.
Comments RSS feed | Trackback URI

If you really want to get crazy you can overload the __set and __get magic functions for a class you define. This can lead to interesting things such as “read-only” variables, for example
readOnlyVariable = $param; } public function __set($name, $value){ if($name == "readOnlyVariable"){ trigger_error("Fo::readOnlyVariable cannot be set in this manner"); return; } $this->$name = $value; } public function __get($name){ return $this->$name; } } var $fo = new Fo($param); //This is valid echo Fo->readOnlyVariable; //This triggers error Fo->readOnlyVariable = "SomeValue"; ?>I’ve also used this method to set and get values from arrays stored in the class I wrote so that it “appeared” (to the coder using the class) that the array’s elements were regular public members of the class that contained said array. That may sound a bit nonsensical but it was very helpful when I needed to both describe a database table in detail (type, length, etc of the table’s columns) with a php array and also have storage for values to insert into said table.
set & get magic functions are going to be included in my 3rd php tricks post :). Interesting usage you had there - some of these lesser-known php tricks can create some pretty interesting code.
Bah, I wrote this comment when I was very tired so the code is riddled with mistakes but if you know anything about PHP you should spot them easy, for some reason it cut off the class def, it was class “Fo” which I’m sure most people understood. The important part is that __set and __get function defs are shown.
The ampersand pointer reference is useful, but doesn’t always work in PHP. This shows up especially when passing variables by reference to functions and when using pointers in foreach() loops–sometimes it works and sometimes it doesn’t. It almost never works when it comes to passing variables by reference repeatedly to recursive functions.
On the whole, I try to avoid using it. If I want to be 100% sure that I’m changing the value of a variable, I modify the original, not the reference. PHP IS crazy…