How can parse_url() and parse_str() functions be utilized for URL validation in PHP?

When validating URLs in PHP, the parse_url() function can be used to break down a URL into its components such as scheme, host, path, etc. The parse_str() function can then be used to parse the query string parameters of the URL. By utilizing these functions, you can easily validate the structure and content of a URL.

$url = "https://www.example.com/page.php?param1=value1&param2=value2";

// Validate URL structure
$url_components = parse_url($url);
if (!$url_components) {
    echo "Invalid URL";
    exit;
}

// Validate query string parameters
$query_string = $url_components['query'];
parse_str($query_string, $query_params);
if (empty($query_params)) {
    echo "No query parameters found";
} else {
    // Additional validation logic for query parameters
}