Content · Web Development · PHP

PHP Closures: Capturing Variables by Value and Reference

PHP Closures: Capturing Variables by Value and Reference: Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.

The examples below begin with the core behavior, then move to an application-oriented use.

How PHP Closures: Capturing Variables by Value and Reference Works


Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

$status = "draft";
$byValue = function () use ($status): string { return $status; };
$status = "published";
echo $byValue();

Output:

draft

Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

$status = "draft";
$byReference = function () use (&$status): string { return $status; };
$status = "published";
echo $byReference();

Output:

published

Reference capture introduces shared mutable state, so avoid it unless the callback truly needs to update external state.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

$counter = 2;
$byValue = function () use ($counter): int { return $counter; };
$byReference = function () use (&$counter): int { return $counter; };
$counter = 8;
echo $byValue() . ":" . $byReference();

Output:

2:8

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • Reference capture introduces shared mutable state, so avoid it unless the callback truly needs to update external state.
  • Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
PHP Closures: Capturing Variables by Value and ReferenceCapturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.
Alternative approachArrow functions implicitly capture used values by value and cannot use the explicit use reference syntax.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of PHP Closures: Capturing Variables by Value and ReferenceReference capture introduces shared mutable state, so avoid it unless the callback truly needs to update external state.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyArrow functions implicitly capture used values by value and cannot use the explicit use reference syntax.

Key Takeaways


  • Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.
  • Reference capture introduces shared mutable state, so avoid it unless the callback truly needs to update external state.
  • Use the example that matches the values, runtime version and dependencies in your application.

PHP Closures: Capturing Variables by Value and Reference is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.

Share this resourceSend this page to someone who may find it useful.