Bifalgorithm post · Computer science one · Passing By Reference
How does PHP pass arguments by reference?
How does PHP pass arguments by reference? (Computer science one · Passing By Reference) — "By default, all types (including numbers, strings, etc.) are passed…
Ideas in this flowchart
- PHP argument passing: by value versus by reference
- By default, all types, including numbers and strings, are passed by value.
- Passing arguments by reference requires different function-definition syntax.
- Place an ampersand (&) before each referenced parameter; no other syntax is necessary.
- When the function is called, PHP automatically manages referencing and dereferencing.
- The code example compares swap() with swapByRef() using $x = 10 and $y = 20.
- swap() passes copies by value, so swapping affects only the parameter copies and leaves $x and $y unchanged.
- swapByRef() passes both parameters by reference, so swapping inside the function changes the original variables.
- The reference syntax differs from the operator used for pointers in C.