How does the PathInfo variable work in PHP for handling virtual paths?

The PathInfo variable in PHP is used to extract information about the virtual path of a URL. It can be particularly useful for handling dynamic URLs and routing requests to the appropriate resources. To use the PathInfo variable, you can simply access it in your PHP code and parse the information as needed.

// Example of using PathInfo variable to handle virtual paths
$pathInfo = $_SERVER['PATH_INFO'];

if ($pathInfo == '/about') {
    // Display the about page
    echo 'This is the about page';
} elseif ($pathInfo == '/contact') {
    // Display the contact page
    echo 'This is the contact page';
} else {
    // Handle other paths or display a 404 error
    header("HTTP/1.0 404 Not Found");
    echo '404 - Page not found';
}