PHP as Keyword
last modified November 25, 2026
The PHP as keyword binds values in a foreach loop and introduces aliases in imports and trait adaptations. The same word covers all three, but each form has its own rules.
This article shows the loop forms, the difference between a copied value and a reference, and how class, function, and constant imports are renamed. It also clears up a common mistake: catch does not use as.
Basic definitions
The as keyword appears in three places in PHP. In a foreach loop it binds the current element to a variable, in an import it introduces an alias, often a shorter name, and in a trait adaptation it gives a method a second name.
In foreach the short form is as $value and the key-value form is as $key => $value. A leading & on the variable, as in as &$value, makes it a reference to the element instead of a copy. The loop itself is covered in PHP foreach loop and PHP foreach keyword.
In an import the keyword renames what is being imported: use Shop\Order as ShopOrder;, use function Library\formatName as upper;, and use const Library\VERSION as LIB_VERSION; are all valid. Group use declarations accept the same syntax. The PHP use keyword article covers the import statement as a whole.
A trait adaptation uses as to add an alias or to change visibility: use Greets { greet as protected hello; }. The trait articles explain that form, so this page only mentions it.
One construct that does not use as is catch. PHP binds the exception with a plain variable: catch (TypeError $e). Writing as there is a syntax error, unlike in some other languages.
foreach over values
The first example walks a list and binds each element to a variable with as.
<?php
declare(strict_types=1);
$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
echo $color, "\n";
}
The loop visits the elements in insertion order, so the output follows the order of the array. $color is a copy of the current element: changing it inside the loop would not change the array. When the keys are not needed, the short form is the clearest way to write the loop.
$ php as_foreach.php red green blue
Keys and values
The two-variable form binds both parts of each entry. It works for indexed arrays, where the keys are integers, and for associative arrays, where they are strings.
<?php
declare(strict_types=1);
$config = [
"host" => "localhost",
"port" => 5432,
"user" => "app",
];
foreach ($config as $key => $value) {
echo $key, " = ", $value, "\n";
}
Each iteration assigns the key to $key and the value to $value. Keeping both sometimes avoids a second lookup: printing the key and the value together is cheaper and clearer than fetching $config[$key] again. The names are ordinary variables, so they are still in scope after the loop.
$ php as_pairs.php host = localhost port = 5432 user = app
A copy or a reference
The plain form of as copies the value. The reference form writes through to the array, which is the only way a foreach loop can change the elements in place.
<?php
declare(strict_types=1);
$prices = ["apple" => 1.0, "pear" => 2.0];
foreach ($prices as $fruit => $price) {
$price *= 2;
}
echo "After copy: ", $prices["apple"], "\n";
foreach ($prices as $fruit => &$price) {
$price *= 2;
}
echo "After reference: ", $prices["apple"], "\n";
Doubling $price in the first loop changes only the copy, so the price of an apple stays 1. The second loop declares &$price, and the same multiplication now updates the element in the array, which is why the final value is 2. The reference remains bound to the last element after the loop, so a later loop should use a different variable name instead of reusing $price.
$ php as_copy_reference.php After copy: 1 After reference: 2
Aliasing a class import
The keyword also gives an imported class a local name. This is useful when two packages define a class with the same short name, or when the original name is long.
<?php
declare(strict_types=1);
namespace Shop {
class Order
{
public function total(): float
{
return 49.90;
}
}
}
namespace App {
use Shop\Order as ShopOrder;
$order = new ShopOrder();
echo "Total: ", number_format($order->total(), 2), "\n";
}
Inside the App namespace, ShopOrder is the name the code uses, while the class itself remains Shop\Order. The alias exists only in the file and namespace block that declares it, so it does not leak into other files. The import and group use syntax are described in PHP use keyword, and PHP namespaces explains the surrounding rules.
$ php as_alias.php Total: 49.90
Aliasing functions and constants
Functions and constants can be imported with their own keywords, and as renames them in the same way.
<?php
declare(strict_types=1);
namespace Library {
function formatName(string $name): string
{
return strtoupper($name);
}
const VERSION = "1.0";
}
namespace App {
use function Library\formatName as upper;
use const Library\VERSION as LIB_VERSION;
echo upper("alice"), "\n";
echo LIB_VERSION, "\n";
}
The call upper("alice") resolves to Library\formatName(), and LIB_VERSION resolves to the constant. The prefixed use function and use const forms are required for these two kinds of import; a plain use imports classes and namespaces only. Short aliases help when a library has long descriptive names, but a name like upper should still say what the function does.
$ php as_function_alias.php ALICE 1.0
Best practices
Use as to make code shorter without making it less clear.
- Choose foreach variable names that describe one element, such as
$orderrather than$o. - Use the key-value form only when the key is used in the body.
- Reach for
as &$valueonly when the loop must modify the array, and use a fresh variable name. - Alias an import when it resolves a conflict or shortens a genuinely long path, not to hide where a class comes from.
- Remember that
catchtakes a plain variable and noaskeyword.
For destructuring inside the loop, such as foreach ($rows as [$name, $score]), see the list destructuring section of PHP foreach loop. Trait method aliasing belongs to PHP use keyword and PHP traits.
Source
PHP manual: importing and aliasing
This article covered the PHP as keyword: iteration over values and key-value pairs, references, and aliases for classes, functions, and constants. Continue with PHP foreach loop for destructuring and nested loops, or with PHP use keyword for the full import statement.
Author
List all PHP tutorials.