as
As a PHP developer, you may have come across the "as" keyword in your code, especially when dealing with arrays or object properties. This powerful keyword
The PHP as Keyword
as is a small but versatile PHP keyword. It is not a function and never appears on its own — it always works as a binder that gives a temporary name to something. You will meet it in four distinct places:
- In a
foreachloop, to bind each element (and optionally its key) to a variable. - In a
usestatement, to give an imported namespace, class, or function a shorter alias. - In a trait
useblock, to alias or re-scope a method during conflict resolution. - In
list()/ array-destructuring inside aforeach(foreach ($rows as [$a, $b])).
This guide covers each use with runnable examples, plus the common gotchas.
as in a foreach loop
This is the use you will see most often. Inside foreach, as binds the current element to a variable for the body of the loop. There are two forms:
<?php
// Value only
foreach ($array as $value) {
// $value is the current element
}
// Key and value
foreach ($array as $key => $value) {
// $key is the current key, $value is the current element
}The variable after as is re-assigned on every iteration; it does not need to exist beforehand.
Iterating values and key/value pairs
<?php
// Indexed array — value only
$names = ["John", "Doe"];
foreach ($names as $name) {
echo $name . "\n";
}
// John
// Doe
// Associative array — key => value
$person = ["name" => "John", "age" => 30];
foreach ($person as $property => $value) {
echo "$property: $value\n";
}
// name: John
// age: 30Because PHP objects are iterable too, the same key => value form walks an object's public properties:
<?php
class Person {
public $name = "John";
public $age = 30;
private $secret = "hidden"; // not visible from outside
}
foreach (new Person() as $property => $value) {
echo "$property: $value\n";
}
// name: John
// age: 30Modifying elements with as &$value
Place a & before the loop variable to bind it by reference so changes propagate back into the original array:
<?php
$nums = [1, 2, 3];
foreach ($nums as &$n) {
$n *= 2;
}
unset($n); // important — see gotcha below
print_r($nums);
// Array ( [0] => 2 [1] => 4 [2] => 6 )Gotcha: after a
foreach (... as &$value)loop,$valuestill references the last element. Alwaysunset($value)afterwards, or a later assignment to$valuewill silently overwrite that last array element.
Destructuring with as [...]
Since PHP 7.1 you can destructure each row directly in the as clause, which is perfect for arrays of pairs or rows:
<?php
$pairs = [[1, "one"], [2, "two"]];
foreach ($pairs as [$num, $word]) {
echo "$num => $word\n";
}
// 1 => one
// 2 => twoThe older list() syntax — foreach ($pairs as list($num, $word)) — does the same thing. See list() for details.
as for namespace and import aliasing
In a use statement, as renames an imported symbol. This avoids long fully-qualified names and resolves clashes when two imports share a short name:
<?php
use App\Models\User as UserModel;
use Acme\Auth\User as AuthUser;
// Both "User" classes are now usable without collision:
$a = new UserModel();
$b = new AuthUser();Aliasing works the same way for imported functions and constants (use function ... as, use const ... as). See PHP namespaces for the full picture.
as for trait conflict resolution
When a class uses two traits that define a method with the same name, as lets you alias one of them so both remain reachable:
<?php
trait Logger { public function report() { return "log"; } }
trait Notifier { public function report() { return "notify"; } }
class Service {
use Logger, Notifier {
Logger::report insteadof Notifier; // pick Logger's report()
Notifier::report as notify; // keep Notifier's under a new name
}
}
$s = new Service();
echo $s->report() . "\n"; // log
echo $s->notify() . "\n"; // notifyHere as does not iterate anything — it simply renames a method. as can also change a method's visibility, e.g. report as protected reportInternal;.
When to use which
| Context | What as does | Example |
|---|---|---|
foreach | binds each element to a variable | foreach ($items as $item) |
use | aliases an imported symbol | use Long\Name as Short; |
trait use block | aliases / re-scopes a method | Notifier::report as notify; |
| destructuring | unpacks each row | foreach ($rows as [$a, $b]) |
Common mistakes
- Using
asoutside the contexts above.asis not a general operator —$x = $y as $z;is a syntax error. - Forgetting
unset()after a by-reference loop (see the gotcha above). - Expecting private properties when iterating an object. Only properties visible in the current scope are exposed.
Summary
The as keyword always binds a name: a loop element in foreach, an alias in a use import or trait block, or a destructured value. Knowing all four uses means you will read and write idiomatic PHP without surprises. To go deeper, explore PHP loops, foreach, namespaces, and traits.