Reference Variables
Is & an explicit alias or implicit sharing — PHP reference variables against the reference models of 10 languages.
C C23
#include <stdio.h>
/* C has no reference variables: pointers are the only way to share a
mutable scalar across names and function boundaries */
void bump(int *n) { (*n)++; }
int main(void) {
int a = 1;
int *b = &a; /* a pointer, not an alias */
*b = 99;
printf("%d\n", a);
int x = 10;
bump(&x);
printf("%d\n", x);
return 0;
}
99 11
C++ C++26
#include <cstdio>
// C++ has both pointers and true references: an lvalue reference is a
// permanent alias, the closest cousin of PHP's &$variable
void bump(int &n) { n++; }
int main() {
int a = 1;
int &b = a; // a true alias: no dereference syntax needed
b = 99;
std::printf("%d\n", a);
int x = 10;
bump(x);
std::printf("%d\n", x);
}
99 11
C# v11.0.100
void Bump(ref int n) { n++; }
var a = 1;
ref int b = ref a; // ref local: a true alias, like $b = &$a
b = 99;
Console.WriteLine(a);
var x = 10;
Bump(ref x);
Console.WriteLine(x);
99 11
Go v1.27.4
package main
import "fmt"
// Go has pointers, not reference variables: taking an address and
// dereferencing are explicit, and there is no implicit aliasing
func bump(n *int) { *n++ }
func main() {
a := 1
b := &a // a pointer: explicit address, explicit dereference
*b = 99
fmt.Println(a)
x := 10
bump(&x)
fmt.Println(x)
}
99 11
Java v27.0.1
public class Main {
static void bump(int[] n) { n[0]++; } // a container carries mutability
public static void main(String[] args) {
int[] a = {1};
int[] b = a; // reference types share one object — but there is no
// way to alias the primitive inside it
b[0] = 99;
System.out.println(a[0]);
int[] x = {10};
bump(x);
System.out.println(x[0]);
}
}
99 11
JavaScript v26.13.1
// no reference variables: objects are implicitly shared, primitives are not
const a = [1];
const b = a; // the same array object behind both names
b[0] = 99;
console.log(a[0]);
function bump(box) { box[0] += 1; }
const x = [10];
bump(x);
console.log(x[0]);
99 11
PHP v8.6.0
// PHP references are explicit: &$b binds to the same variable slot as $a
$a = 1;
$b = &$a; // from here the two names are one storage
$b = 99;
echo $a, "\n";
// by-reference parameters declare the sharing in the signature
function bump(&$n) { $n++; }
$x = 10;
bump($x);
echo $x, "\n";
99 11
Python v3.15.2
def bump(box):
box[0] += 1 # no reference syntax: a mutable container carries it
a = [1]
b = a # the name binds to the same list object
b[0] = 99
print(a[0])
x = [10]
bump(x)
print(x[0])
99 11
Rust v1.99.1
// &mut is an exclusive borrow: the compiler guarantees no aliasing while
// it is live — the philosophical opposite of PHP's freely shareable &
fn bump(n: &mut i32) { *n += 1; }
fn main() {
let mut a = 1;
{
let b = &mut a; // one exclusive mutable borrow
*b = 99;
} // the borrow ends here
println!("{}", a);
let mut x = 10;
bump(&mut x);
println!("{}", x);
}
99 11
TypeScript v26.13.1
// no reference variables: objects are implicitly shared, primitives are not
const a: number[] = [1];
const b = a; // the same array object behind both names
b[0] = 99;
console.log(a[0]);
function bump(box: number[]): void { box[0] += 1; }
const x = [10];
bump(x);
console.log(x[0]);
99 11
Language differences
C: pointers (example): &x to take an address, *p to dereference, all explicit; no reference type exists, so a pointer is the only road to sharing a mutable scalar; pointers can re-target, be null-checked, and do arithmetic — flexibility paid for with undefined behavior.
C++: pointers plus true references (example): int& is a permanent alias of a variable (rebinding impossible, no dereference syntax), semantically nearly identical to PHP references; the differences are that C++ references cannot rebind, cannot form arrays, and live by scope rules, while PHP references can be unbound via unset and are reference-counted.
C#: ref parameters plus ref locals (example): ref int b = ref a creates a true alias (C# 7+), and Bump(ref x) must mark ref at both the signature and the call site — the sharing is explicit in two places; out (must assign) and in (read-only ref) complete the family; plain parameters remain by-value.
Go: pointers only (example): &a and *b are explicit and plain; no reference-variable concept, and by-reference passing means writing pointer types and taking addresses at the call site; no pointer arithmetic, and escape analysis decides heap vs stack.
Java: reference types only (example): a variable always holds a value (primitives) or a copy of a reference (objects), and arguments pass that copy — reassignment in the callee never affects the caller; to mutate a scalar across functions you reach for single-element arrays, AtomicInteger, or fields; no language-level reference parameters.
JavaScript: no reference variables (example): objects pass by shared reference (a copied reference), primitives by value — the same semantics as Java; no reference-parameter syntax, so mutating a scalar across functions takes a wrapper object or a return value.
PHP: explicit references (example): &$b binds a name to the same variable slot (reference-counted sharing), and unset detaches only the name, not the data; the signature's &$n declares by-reference passing with nothing needed at the call site; references work in foreach (as &$v), function returns (&func()); note that references break copy-on-write arrays — demonstrated in this series' COW entry.
Python: no reference variables (example): a variable is a name bound to an object, and assignment shares the object reference without creating a variable alias — immutable scalars (int/str) cannot be mutated through any alias; mutable containers are shared by nature; function arguments pass object references by value.
Rust: &mut exclusive borrows (example): no other borrow may exist while a mutable borrow is live — aliasing XOR mutability is the core borrow-checker creed; philosophically the opposite of PHP's free references: PHP permits arbitrary aliasing, Rust makes shared mutable state fail to compile; interior mutability goes through RefCell/Cell with runtime checks.
TypeScript: at the language level the same as JS (example): objects shared, primitives copied; the type system does not change runtime reference semantics; there is no ref/out parameter type, so cross-function mutable sharing rides on object or array types.