How can the functions parse_url() and http_build_query() be helpful in PHP?
parse_url() can be helpful in PHP to extract various components of a URL, such as the scheme, host, path, query, and fragment. http_build_query() can be useful to build a query string from an array of parameters, making it easier to construct URLs with query parameters.
$url = "https://www.example.com/page.php?name=John&age=30";
$url_components = parse_url($url);
echo "Scheme: " . $url_components['scheme'] . "\n";
echo "Host: " . $url_components['host'] . "\n";
echo "Path: " . $url_components['path'] . "\n";
$query_params = array(
'name' => 'Jane',
'age' => 25
);
$query_string = http_build_query($query_params);
$new_url = "https://www.example.com/new_page.php?" . $query_string;
echo "New URL: " . $new_url;