Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

UsmanBasharmal's avatar

what is the difference between these two statements in php?

I was reading about arrow functions in php documentions and I came across this code

 fn($x = 42) => $x;

  fn(&$x) => $x; 

what is the difference between these two statements and what they do ?

0 likes
3 replies
jlrdw's avatar

& sets a reference to the variable. Top one is probably an updated at PHP 8 version of same.

sr57's avatar

fn($x = 42) : is a function with one or 0 parameter, if 0, default value 42

fn(&$x) : is a function with one parameter passed as reference, meaning if changed in the function, the new value of $x will be pass in the main.

Please or to participate in this conversation.