How can the issue of a URL path not being selected in a select box be resolved in PHP?

Issue: The URL path is not being selected in a select box because the value of the select options does not match the URL path. To resolve this, we can use PHP to check the URL path and set the selected attribute for the corresponding option in the select box.

<?php
// Get the current URL path
$current_path = $_SERVER['REQUEST_URI'];

// Define the options for the select box
$options = [
    '/option1' => 'Option 1',
    '/option2' => 'Option 2',
    '/option3' => 'Option 3'
];

// Display the select box with the correct option selected
echo '<select name="options">';
foreach ($options as $path => $label) {
    $selected = ($current_path == $path) ? 'selected' : '';
    echo '<option value="' . $path . '" ' . $selected . '>' . $label . '</option>';
}
echo '</select>';
?>