Code: Select all
<?php
function foo($var) {
$var = "inside";
return $var;
}
$tar = foo($bar);
echo $tar . "<br />";
?>Moderator: General Moderators
Code: Select all
<?php
function foo($var) {
$var = "inside";
return $var;
}
$tar = foo($bar);
echo $tar . "<br />";
?>Code: Select all
function foo(&$var) {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"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 itCode: Select all
1. <?php
2.
3. function foo() {
4. $var = "inside";
5. return $var;
6. }
7.
8. $tar = foo();
9. echo $tar . "<br />";
10. ?>