ZetCode

PHP as Keyword

last modified April 16, 2025

The PHP as keyword has two primary uses: in foreach loops to assign array elements to variables, and in namespace aliasing to create shorter names for classes. This tutorial covers both uses with practical examples.

Basic Definitions

In foreach loops, as assigns the current element to a variable during iteration. The syntax is foreach ($array as $value) or foreach ($array as $key => $value) for key-value pairs.

For namespaces, as creates an alias for a class or namespace. The syntax is use Namespace\Class as Alias. This helps avoid name conflicts and makes code cleaner.

The as keyword is also used in try-catch blocks to assign exceptions to variables, though this is less common than the other uses.

Basic foreach with as

This example demonstrates the simplest use of as in a foreach loop.

basic_foreach.php
<?php

declare(strict_types=1);

$colors = ['red', 'green', 'blue'];

foreach ($colors as $color) {
    echo $color . "\n";
}

The code iterates through the $colors array. Each element is assigned to $color via the as keyword. The loop body then uses this variable. This is the most common foreach pattern.

foreach with Key-Value Pairs

This example shows how to access both keys and values using as.

key_value_foreach.php
<?php

declare(strict_types=1);

$user = [
    'name' => 'John',
    'age' => 25,
    'city' => 'New York'
];

foreach ($user as $key => $value) {
    echo "$key: $value\n";
}

The code loops through an associative array. The as keyword assigns both key and value to variables. This syntax is essential for working with key-value data structures like configuration arrays.

Namespace Aliasing with as

This example demonstrates creating shorter names for classes using as.

namespace_alias.php
<?php

declare(strict_types=1);

namespace MyApp\Services;

class LongClassName {
    public function greet() {
        echo "Hello from LongClassName!\n";
    }
}

// In another file:
use MyApp\Services\LongClassName as LCN;

$obj = new LCN();
$obj->greet();

The code defines a class with a long name. The use statement with as creates a shorter alias. This makes the code cleaner and easier to maintain, especially with long namespace paths.

Aliasing in Group Use Declarations

This example shows how to alias multiple classes in a group use statement.

group_use_alias.php
<?php

declare(strict_types=1);

namespace Vendor\Package;

class FirstClass {}
class SecondClass {}

// In another file:
use Vendor\Package\{
    FirstClass as FC,
    SecondClass as SC
};

$first = new FC();
$second = new SC();

The code demonstrates PHP 7+ group use syntax with aliasing. Multiple classes can be imported and aliased in one statement. This is useful when working with many classes from the same namespace.

Exception Handling with as

This example shows the less common use of as in try-catch blocks.

exception_as.php
<?php

declare(strict_types=1);

try {
    $result = 10 / 0;
} catch (DivisionByZeroError as $e) {
    echo "Caught exception: " . $e->getMessage() . "\n";
}

The code catches a division by zero error. The as keyword assigns the exception object to $e. This allows accessing exception methods and properties. While less common, this is valid PHP syntax.

Iterating Objects with foreach

This example demonstrates using as to iterate over object properties.

object_iteration.php
<?php

declare(strict_types=1);

class User {
    public $name = 'John';
    public $age = 30;
    private $id = 12345;
}

$user = new User();

foreach ($user as $property => $value) {
    echo "$property: $value\n";
}

The code loops through a User object's public properties. The as keyword assigns each property name and value to variables. Note that private properties are not accessible in this way by default.

Using as with Array References

This example shows how to modify array elements directly using references.

reference_foreach.php
<?php

declare(strict_types=1);

$numbers = [1, 2, 3, 4];

foreach ($numbers as &$number) {
    $number *= 2;
}

print_r($numbers);

The code doubles each value in the array. The & before $number makes it a reference to the actual array element. Changes to $number affect the original array. This is a powerful but potentially dangerous feature.

Best Practices

Source

PHP foreach Documentation

This tutorial covered the PHP as keyword with practical examples showing foreach iteration, namespace aliasing, and exception handling scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP basics tutorials.