Which PHP function name changes parameter values?

I noticed that some php functions are allowed to change the value of variables just by calling the function, while others are not. For example, consider trim()

and sort()

that perform "action:"

//trim()
$string = "     Test";
echo $string."<br>";
trim($string);
echo $string."<br>";
//each echo returns the same. trim() does nothing for the second echo

      

However, with sort():

//sort()
$fruits = ['squash','apple','kiwi'];
foreach($fruits as $fruit){
    echo $fruit."<br>";
    //returns array in original order
}
sort($fruits); 
foreach($fruits as $fruit){
    echo $fruit."<br>";
    //returns sorted array
}

      

I know the correct way to use both (did it 1000 times). But what is the technical term for the difference between how these two functions work? sort()

modifies its' variable (to some extent), but trim()

does not.

+3


source to share


2 answers


Here are some docs about these two php functions. You can see that the trim parameter is trans by value and the collation is by reference.

For meaning and reference see Pedro Lobito's answer or here



bool sort(array &array_arg [, int sort_flags])

string trim  ( string $str  [, string $charlist  ] )

      

+2


source


You can read What's the Difference Between Pass by Reference or Pass by Value?



$string = trim($string);

      

0


source







All Articles