3

I noticed that some php functions have the power to change a variables value by simply calling the function and others do not. For instance, consider trim() and sort() which both perform an "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 (done it a 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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Shawn Cooke
  • 907
  • 1
  • 11
  • 28
  • 2
    Technical terms are `passing by reference/passing by value` – u_mulder May 18 '17 at 14:20
  • It feels that way to me too but I don't need to use the `&` sign. Is the function automatically doing that? – Shawn Cooke May 18 '17 at 14:24
  • These you mention are native PHP functions so they are written in the C programming language and PHP syntax doesn't apply. You need `&` in your own functions written in PHP. – Álvaro González May 18 '17 at 14:27
  • 2
    Whether a parameter is accepted by reference or by value is a choice of the *function definition*, not the *function call*, so it doesn't matter if the function you're calling is internal or user-defined, you won't write the `&` to call it. (Before PHP 5.4, it was *possible* to *force* a parameter into a function by reference; this was weird, a bad idea, and is no longer possible in any supported version of PHP.) – IMSoP May 18 '17 at 14:29
  • @IMSoP He does know the difference (mostly) and is just asking for the name of the feature. However, he has already accepted an answer and linking to that other question can help others to expand the information. I think a dupe link can do better. – Álvaro González May 18 '17 at 15:17

2 Answers2

2

Here is some docs about these two function of php. You can see trim's parameter is trans by value, while sort's parameter is by reference.

For value and reference refer to Pedro Lobito's answer or here

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

string trim  ( string $str  [, string $charlist  ] )
Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 1
    Just to clarify, that's not "the source code", it's just some documentation; and two different formats of documentation, at that. The actual source code is written in C, using all sorts of macros and internal data structures, so wouldn't be all that useful as a reference in this case. – IMSoP May 18 '17 at 15:46
  • @IMSoP, thank you. My statement is not precise here, I do some modify. – LF00 May 18 '17 at 15:49
0

You may want to read What's the difference between passing by reference vs. passing by value?

$string = trim($string);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268