PHP pass-by-reference question

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

The way I understand passing-by-reference is this...

PHP Code: [ Select ]
<?php
function concatenate(&$var, $text)
{
    $var .= $text;
   
    return strlen($var);
}
 
$datum = "To be or not to be?";
 
$len = concatenate($datum, ' That is the question!!');
echo $datum . '<br /><br /><br />' . $len;
?>
  1. <?php
  2. function concatenate(&$var, $text)
  3. {
  4.     $var .= $text;
  5.    
  6.     return strlen($var);
  7. }
  8.  
  9. $datum = "To be or not to be?";
  10.  
  11. $len = concatenate($datum, ' That is the question!!');
  12. echo $datum . '<br /><br /><br />' . $len;
  13. ?>
It produces the following result
Quote:
To be or not to be? That is the question!!


42

I understand that much and it makes sense... I'm reading a JavaScript pass-by-reference and that got me thinking about PHP. I've done this one before when I needed two results from one functions and tried to figure out how to do that since I can't have two returns in one function.

Anyway, that is pass-by-reference... is it possible to copy a reference or compare a reference in PHP? Or is it only to pass the reference to the function that is available in PHP?

Just wondering if PHP has those features as well... could be useful.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Okay, there is copy and the code for that is...
PHP Code: [ Select ]
<?php
function concatenate(&$var, $text)
{
    $var .= $text;
   
    return strlen($var);
}
 
$datum = "To be or not to be?";
 
$len = concatenate($datum, ' That is the question!!');
echo $datum . '<br /><br /><br />' . $len;
 
$ref =& $datum;
 
$ref .= ' Or is it?';
 
echo '<br /><br /><br />' . $datum . '<br /><br /><br />' . $ref;
?>
  1. <?php
  2. function concatenate(&$var, $text)
  3. {
  4.     $var .= $text;
  5.    
  6.     return strlen($var);
  7. }
  8.  
  9. $datum = "To be or not to be?";
  10.  
  11. $len = concatenate($datum, ' That is the question!!');
  12. echo $datum . '<br /><br /><br />' . $len;
  13.  
  14. $ref =& $datum;
  15.  
  16. $ref .= ' Or is it?';
  17.  
  18. echo '<br /><br /><br />' . $datum . '<br /><br /><br />' . $ref;
  19. ?>
Which gave me
Quote:
To be or not to be? That is the question!!


42


To be or not to be? That is the question!! Or is it?


To be or not to be? That is the question!! Or is it?

One thing baffles me: is there such thing as comparing references in PHP?

Or is it simply:
PHP Code: [ Select ]
if($ref == $datum) { return true; }

Is that what comparing by references is? Sounds too simple :lol:
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

There is Return by reference. That sounds interesting. I guess I don't know PHP as much as I thought I did :D

Time for me to by the O'Reilly PHP book :lol:
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Technically you're not comparing the references, you're comparing the variables the references point to.

Even if you compare your initial string with your reference using strict equality (===) it will evaluate to true.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

joebert wrote:
Technically you're not comparing the references, you're comparing the variables the references point to.

Even if you compare your initial string with your reference using strict equality (===) it will evaluate to true.

That's what I thought... thanks for confirming it, I just didn't know if there was more to it or something special about it.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 60 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.