debug_zval_dump
debug_zval_dump(mixed $value): void
Dumps a variable's internal zval details (type, value, and reference count) — the debug window into PHP's copy-on-write machinery.
C C23
#include <stdio.h>
int main(void) {
// C is strictly value semantics: a copy is a new storage cell
int x = 1;
int y = x; // value copy
int *px = &x, *py = &y;
printf("copied ints share storage: %s\n", px == py ? "true" : "false");
// arrays decay to pointers: passing one aliases the original storage
int arr[2] = {1, 2};
int *pa = arr;
printf("array alias shares storage: %s\n", pa == (int *)&arr ? "true" : "false");
return 0;
}
copied ints share storage: false array alias shares storage: true
C++ C++26
#include <iostream>
int main() {
int x = 1;
int y = x; // value copy: new storage
int &r = x; // reference: another name for the same cell
std::cout << "copied ints share storage: " << (&x == &y ? "true" : "false") << "\n";
std::cout << "reference shares storage: " << (&r == &x ? "true" : "false") << "\n";
return 0;
}
copied ints share storage: false reference shares storage: true
C# v11.0.100
// C# observes value vs reference semantics through struct/class behaviour
var a = new Counter { N = 1 };
var b = a; // class: copies the reference, same object behind
b.N = 42;
Console.WriteLine($"reference copy sees the change: {a.N == 42}");
var s1 = (N: 1, Tag: "t");
var s2 = s1; // tuple (struct): copies the value
s2.N = 42;
Console.WriteLine($"value copy sees the change: {s1.N == 42}");
class Counter
{
public int N;
}
reference copy sees the change: True value copy sees the change: False
Go v1.27.4
package main
import "fmt"
func mutateInt(n int) { n = 42 } // int is copied on call
func mutateSlice(s []int) { s[0] = 42 } // slice header copied, backing array shared
func main() {
x := 1
mutateInt(x)
fmt.Println("int passed by value:", x == 1)
nums := []int{1, 2}
mutateSlice(nums)
fmt.Println("slice shares the backing array:", nums[0] == 42)
}
int passed by value: true slice shares the backing array: true
Java v27.0.1
public class Main {
static void mutateInt(int n) { n = 42; } // int is copied on call
static void mutate(StringBuilder sb) { sb.append("!"); } // reference copied, object shared
public static void main(String[] args) {
int x = 1;
mutateInt(x);
System.out.println("int passed by value: " + (x == 1));
StringBuilder sb = new StringBuilder("hi");
mutate(sb);
System.out.println("object reference shares the instance: " + sb.toString().equals("hi!"));
}
}
int passed by value: true object reference shares the instance: true
JavaScript v26.13.1
let scalar = 1;
let copy = scalar; // primitives are copied by value
copy = 42;
console.log("scalar copy sees the change:", scalar === 1);
const obj = { n: 1 };
const ref = obj; // objects are copied by reference: same instance
ref.n = 42;
console.log("object alias sees the change:", obj.n === 42);
scalar copy sees the change: true object alias sees the change: true
PHP v8.6.0
$a = range(1, 2); // runtime-built: literal arrays show confusing refcount markers
debug_zval_dump($a); // refcount(2): $a itself + the by-value call argument
$b = $a; // assignment only bumps the refcount, no data copied yet (copy-on-write)
debug_zval_dump($a); // refcount(3): $a, $b, and the call argument
$b[] = 3; // writing to $b triggers the real copy; $a's refcount drops back
debug_zval_dump($a); // refcount(2): $b has detached
array(2) packed refcount(2){
[0]=>
int(1)
[1]=>
int(2)
}
array(2) packed refcount(3){
[0]=>
int(1)
[1]=>
int(2)
}
array(2) packed refcount(2){
[0]=>
int(1)
[1]=>
int(2)
}
Python v3.15.2
def observe() -> None:
# lists are references: assignment aliases the same object
a = [1, 2]
b = a
b.append(3)
print("list alias sees the change:", a == [1, 2, 3])
print("same identity:", a is b)
# ints are immutable: rebinding the name never touches the old value
x = 1
y = x
y = 42
print("int rebind sees the change:", x == 1)
observe()
list alias sees the change: True same identity: True int rebind sees the change: True
Rust v1.99.1
fn main() {
// i32 is Copy: a plain value copy, the original stays usable
let n1: i32 = 1;
let n2 = n1;
println!("Copy type keeps the original usable: {}", n1 == 1 && n2 == 1);
// String is move semantics: assignment transfers ownership (compile-time rule)
let s1 = String::from("hi");
let s2 = s1; // s1 is no longer usable after this move
let s3 = s2.clone(); // clone: an independent allocation
println!("clone is a new allocation: {}", !std::ptr::eq(s2.as_ptr(), s3.as_ptr()));
// println!("{}", s1); // would NOT compile: value moved — the borrow checker is the guard
}
Copy type keeps the original usable: true clone is a new allocation: true
TypeScript v26.13.1
let scalar: number = 1;
let copy: number = scalar; // primitives are copied by value
copy = 42;
console.log("scalar copy sees the change:", scalar === 1);
const obj: { n: number } = { n: 1 };
const ref: { n: number } = obj; // objects alias the same instance
ref.n = 42;
console.log("object alias sees the change:", obj.n === 42);
scalar copy sees the change: true object alias sees the change: true
Language differences
C: no reference-count concept — the demo shows the value-semantics idiom: copies live at different addresses, while an array decays to a pointer and aliases the original storage.
C++: same as C plus references — int& compares equal by address with the original, a copy does not.
C#: no refcount window — class (reference sharing) vs tuple/struct (value copy) side effects demonstrate the two semantics.
Go: arguments are copied on call — an int copy leaves the caller unchanged, while a slice copies only the header and shares the backing array (a Go-specific model, noted in comments).
Java: primitives by value, objects by reference — an int stays unchanged across a call, a StringBuilder mutation is visible to the caller.
JavaScript: primitives copy by value, objects share the same instance via reference — observed through whether a mutation is visible.
PHP: debug_zval_dump is a PHP-specific window into the zval struct with no counterpart in other languages — it prints type/value/refcount/is_ref and is the experimental bench for understanding "PHP assignment is refcounting + copy-on-write"; the refcount reads one more than the visible references because of the call argument; literal strings print interned (compile-time constants carry no count); this entry builds the array with range() at runtime to avoid literal-array constant caching and gets a clean 2→3→2 sequence: assignment only bumps the count, writing triggers the real copy (copy-on-write) — the reason PHP array assignment is cheap; debugging use only.
Python: no refcount window (sys.getrefcount numbers are implementation-dependent and unreliable) — is identity comparison and mutation visibility demonstrate reference semantics vs immutable value semantics.
Rust: the ownership model keeps no refcount visible at runtime — the demo contrasts Copy types (value copies) with move/clone (ownership transfer and deep copy); the borrow checker blocks dangling use at compile time.
TypeScript: same as JS — primitives by value, objects by reference.