Page 1 of 1

Very Basic Question

Posted: Thu Jun 18, 2009 11:58 pm
by pdx_xprs
Here is the code:

Code: Select all

<?php
 
function foo($var) {
 $var = "inside";
 return $var;
}
 
$tar = foo($bar);
echo $tar . "<br />";
?>
When this code executes, it returns "Inside". Here's is what I'm not clear about. On line '8', it seems as if there is no value being passed, per se, to the function when the function is called. Rather, it seems as if $bar is capturing the value returned and assigned to $var on line 5 after the functions is called and does its work. Is this correct?

Re: Very Basic Question

Posted: Fri Jun 19, 2009 2:53 am
by requinix
Mostly correct.

Like in most languages, arguments given to a PHP function are normally by value, which means the value of the variable is used. There is no direct access to the variable itself.
Inside foo(), $var will be a copy of whatever $bar is*. It then modifies the copy on line 4. $bar will not have the new value because it itself was not modified.

You can change this behavior with an &.

Code: Select all

function foo(&$var) {
This means $bar will be passed by reference, meaning a reference is used. On line 4 $var is updated, and since it is a reference to the $bar variable, $bar will be updated too.

Code: Select all

function foo_byvalue($var) {
    $var = "by value";
    return $var;
}
echo foo_byvalue($bar1); // "Notice: Undefined variable: bar1"  "by value"
echo $bar1; // "Notice: Undefined variable: bar1"
$bar1 = "bar";
foo_byvalue($bar1); echo $bar1; // "bar"
 
function foo_byref(&$var) {
    $var = "by reference";
    return $var;
}
echo foo_byref($bar2); // "by reference"
echo $bar2; // "by reference"
$bar2 = "bar";
foo_byref($bar2); echo $bar2; // "by reference"
You have to give a variable when passing by-reference:

Code: Select all

echo foo_byref("a literal string"); // "Fatal error: Only variables can be passed by reference"
 
// for php 5 the rules are still enforced but you get a little leeway
echo foo_byref($bar3 = "a literal string"); // "Strict Standards: Only variables should be passed by reference"  "by reference"
echo $bar3; // "a literal string"
// PHP is complaining since ($bar3 = "a literal string") is technically an expression, not a variable
// however it realizes that it can do the expression and use $bar3 as a variable, thus making everybody happy
// so while this is bad practice, PHP lets you get away with it
* You are (mostly) correct: $bar doesn't have a value because it doesn't exist. But since you forced it to by trying to use it, PHP set a "default value" of null. Thus $var will exist and is_null($var) == true. PHP also issues a warning message when it does this but you might not see it depending on your php.ini settings.

Re: Very Basic Question

Posted: Fri Jun 19, 2009 10:20 am
by pickle
While ~tasairis certainly was well thought out, I think the actual solution is simpler.

$bar has no value. It's not declared anywhere, nor assigned anything. In the end though, that doesn't matter, because foo() doesn't care. You can set $bar to "Your mother smells of elderberries" for all the good it'll do. That value will get assigned to $var in the foo() function declaration. However, immediately after, foo() assigns $var the value of "inside". It then returns $var, with the value of "inside", which gets stored in $tar and output.

Re: Very Basic Question

Posted: Fri Jun 19, 2009 10:41 am
by Eric!
Everyone is right, but to put it in terms of your own code: your code is equivalent to the following.

Code: Select all

1. <?php
   2.  
   3. function foo() {
   4.  $var = "inside";
   5.  return $var;
   6. }
   7.  
   8. $tar = foo();
   9. echo $tar . "<br />";
  10. ?>
Sorry I pasted it with the line numbers on..but you get the idea.