Passing by reference:
<?php
$str = "test \n";
trim(&$str);
echo "-" . "$str" . "-";
?>
output is:
-test
-
but when I do
<?php
$str = "test \n";
$str = trim($str);
echo "-" . "$str" . "-";
?>
the output is:
-test-
Why can’t I pass this by reference?
Because
trim()does not expect a reference and thus does not modify the string that was passed to it. Passing a reference only makes sense if the function expects one – and then you do not have the choice of not passing a reference since what matters if the function definition contains a reference argument or not.What you are trying to do, call-time pass-by-reference is deprecated in PHP since a long time. Besides that, even if it wasn’t deprecated it would only work for functions that actually modify the argument.