TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-28-2007, 10:57 AM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Red face Lowdown on Passing Things as Reference

As a boy of 18 learning PHP, many tutorials over-complicated the PHP passing by reference side of things and just became all too puzzling to say the least. Let's start us off with a little quiz.

PHP Code:
$szVar1 'TalkPHP.com';
$szVar2 'WiredFlame.com';

$szVar1 =& $szVar2;
$szVar1 'TalkPHP.com'
In the above code, if I were to echo out $szVar2, what would its value be? If you'd have said to me WiredFlame.com then you'd have been dead wrong, sadly. Surprisingly, TalkPHP.com is the correct answer. Even though we're assigning the value of TalkPHP.com to $szVar1, because we've made $szVar1 as a reference to $szVar2, we can effectively now control $szVar2 as well.

Passing by reference is really quite simple on the face of it. You're not assigning $szVar1 the value of $szVar2, which would have been WiredFlame.com. You are assigning it $szVar2 as a reference.

Essentially now, when $szVar1 changes, $szVar2 follows suit.

You may ask why this is useful. Well, the chances are you've blissfully been using PHP functions out-of-the-box and they've been accepting variables or arrays as reference. These are the functions that just work, they do not return anything, usually - which means you do not have to catch the function's return value like so:

PHP Code:
$aArray array_pop($aArray); 
Albeit some will still return true and false. What you normally do is:

PHP Code:
array_pop($aArray); 
Your $aArray will then be changed for you. This is a good example of an array being passed as a reference, and not by its value.

A good example where you could use passing by reference, is in the foreach loop. Most people would do the following:

PHP Code:
$aArray = array('Raspberries''Oranges''Apples''Kiwis');

foreach(
$aArray as $szKey => $szValue)
{
    
$aArray[$szKey] .= '.';

This would place full-stops on the end of all of our fruits. However, what we could do is pass by reference and use it that way. Like so:

PHP Code:
$aArray = array('Raspberries''Oranges''Apples''Kiwis');

foreach(
$aArray as &$szItem)
{
    
$szItem .= '.';

As we are passing the elements in by reference, from the array, we can set them just by assigning $szItem to the action we wish to perform. PHP will handle the rest and change all the items in our array for us! How so very clever is that?

Of course, you may also pass by reference in functions. This is done like so:

PHP Code:
function array_add_fullstop(&$aArray)
{
    if(
count($aArray) == 0)
    {
        return 
false;
    }
    
    foreach(
$aArray as &$szItem)
    {
        
$szItem .= '.';
    }
    
    return 
true;

You see, we've even gone the extra mile and added in a return as true or false. This can be checked like so:

PHP Code:
if(array_add_fullstop($aArray) === true)
{
    
// Do something fancy!
}
else
{
    
// Do some disastrous!    

You don't need to worry about the array because if the function has returned true then your array has been modified to include the full-stops at the end of each array element.

Passing by reference, as you can see, need not be a complex topic. It's pretty straightforward as aforementioned. The next time you see the ampersands (&) prepended to variables and arrays, as well as in function arguments and loops, just remember, whatever the topic, complex or not, TalkPHP.com is the next best thing to having Albert Einstein sat next to you when you're doing your Maths homework. That'll show those condescending teachers!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:26 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design