Jump to content

PHP Reference


perryc

Recommended Posts

Can someone help me with passing functions by reference in php. having a hard time trying to get a grasp around the idea of reference's and what i would use them for in everyday php coding.

 

 

Thanks,

 

 

 

 

Cody Perry

Link to comment
Share on other sites

Pass by value looks like this:

 

<?php

function test($temp)
{
   $temp++;
}

$i = 0;
test($i); 
echo $i;  // displays "0"

?>

 

There's no connection between the $temp variable within the function and the $i variable outside, so echoing out $i results in "0". $temp only exists within the test() function and disappears once the function ends.

 

Here's pass by reference:

 

<?php

function test(&$test)
{
   $test++;
}

$i = 0;
test($i);
echo $i; // displays "1"

?>

 

Note the "&" before the variable name in test(&$test). That means that when you pass in the variable, rather than creating a brand new variable, $temp references the $i variable outside the function. Any changes made to $temp within the function will also affect $i.

 

How is this helpful? I'm not sure, it depends on what you need coded. I've really never used it in my PHP code (yet, at least) but I have used it quite often in C++.

 

It has come in handy for me on several occasion. For example, consider this situation... You have a function that should return a valid integer (0 through whatever). However, there is also a chance that the number you are looking for doesn't exist. How do you code your function? If the variable doesn't exist and the function fails, you can't return 0 (false), since that is a valid integer and could be confused.

 

However, you could do something like this:

 

<?php

function get_variable($temp)
{
  if (variable exists)
  {
     $temp = variable;
     return true;
  }
  else
  {
     return false;
  }
}

$i = '';
if (get_variable($i))
{
  // get_variable() returns true
  // $i will contain the variable you were looking for
}
else
{
  // get_variable() returns false
  // $i still contains ''
}

?>

 

Does that make a bit more sense?

Link to comment
Share on other sites

A little bit the way the book explains the example is like a matchbox with a string attached to it I got real confused at first. Then i went back and read the information step by step. So i understand it a little bit better now. Im learning Including and Requiring files section on my chapter right now so hopefully i don't have any more trouble with this chapter i fell real stupid sometimes asking questions like these to you guys. Thanks,

 

 

 

 

 

Cody Perry

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...