Skip to content
PHP Logo

What are some resources or documentation that can provide guidance on utilizing optional parameter routing in PHP?

When utilizing optional parameter routing in PHP, it is important to understand how to properly structure your routes to handle both required and optional parameters. One way to achieve this is by using regular expressions in your route definitions to specify optional parameters. This allows for more flexibility in defining routes and handling different URL patterns.

php
// Example of optional parameter routing in PHP using regular expressions
$router->get('/user/{id}/{name?}', function($id, $name = null) {
    // Handle routing logic here
    if ($name) {
        echo "User ID: $id, Name: $name";
    } else {
        echo "User ID: $id";
    }
});