Jump to content

Returning values from a function by reference


wright67uk

Recommended Posts

Im trying to understand the code below. (taken from an O'REILLY book)

 

Im trying to get my head around this, so can anyone tell me if im thinking about this in the right way;

 

The function (fix_names) borrows the values from $a1,$a2 and $a3 and puts them into $n1,$n2, and $n3. It then processes the strings contained within these newly created variables and then returns the processed values back into $a1,$a2,and $a3?

 

The variables $n1,$n2, and $n3 cannot be echoed as they were created within the function.?

 

Please correct me if im wrong.

 

<html> 
<head>
</head>
<body>
<?php

$a1 = "EDWARD";  $a2 = "thomas";  $a3 = "wriGHT";

fix_names($a1,$a2,$a3);
echo $a1." ".$a2." ".$a3;

function fix_names(&$n1,&$n2,&$n3)
{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));
}

?>
</body>
</html>

Link to comment
Share on other sites

- You initially declare and set your variables $a1, $a2, $a3.

- You then call the function fix_names declaring that they are the 3 variables you want processed.

- The variables inside the function are $n1, $n2, $n3, these are only available inside the function, and are set to the values of the 3 variables you declared when you call this function in this case $a1, $a2, $a3.

- The function applies the formatting to the 3 variables and then returns the values to $a1, $a2, $a3 repectively.

- You can call the same function many times like this

 

<?php

$a1 = "EDWARD";  $a2 = "thomas";  $a3 = "wriGHT";
$b1 = "bOB"; $b2 = "FRed"; $b3 = "johN";
$dave = "daVe"; $mark = "MARK"; $frank = "jasOn";

fix_names($a1,$a2,$a3);
echo $a1." ".$a2." ".$a3."<br/>";

fix_names($b1,$b2,$b3);
echo $b1." ".$b2." ".$b3."<br/>";

fix_names($dave,$mark,$frank);
echo $dave." ".$mark." ".$frank."<br/>";

function fix_names(&$n1,&$n2,&$n3)
{
$n1 = ucfirst(strtolower($n1));
$n2 = ucfirst(strtolower($n2));
$n3 = ucfirst(strtolower($n3));
}

?>

 

Try it for yourself :)

Link to comment
Share on other sites

Ok to fully understand what's going on here you must understand that when you pass a variable by reference you are not sending the data contained within that variable but instead sending an address of memory where the data you want is located... this is very different then just sending data.

 

When a function executes you must understand that it has a certain level of scope, ie it cant see/edit variables out side of its { } but if you pass a variable as a reference you allow the function to directly edit a piece of memory.

 

Let me explain with a few examples

 

Example 1

The basics of a function

<?php
$a=10;
$b=addOne($a);  // this is simple and you should understand this

echo $a; //10
echo $b; //11

function addOne($var)
{
return $var ++; // This function cant directly edit $a as it is outside its scope so instead we must "return" the data from the variable 
}
?>

 

Example 2

Using example 1 but instead passing $a as reference.

 

<?php

$a=10;
addOne(&$a);  // I pass $a as a reference using &

echo $a; //11

function addOne($var) // now var doesn't contain 10 but instead it contians the location of $a

{
$var ++;  // this is like doing $a++ but it goes through $var inorder to access $a

}

 

 

I hope this helps you get your head round this, its not that hard like anything in php once you get the concept :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.