Proper URL forming with a query string and an anchor hashtag

To form a proper URL with a query string and an anchor hashtag in PHP, you can use the http_build_query() function to create the query string and concatenate it to the base URL using the ? symbol, followed by the anchor hashtag using the # symbol.

Watch a course Learn object oriented PHP

Example:

<?php

$base_url = "http://www.example.com";
$query_params = ["param1" => "value1", "param2" => "value2"];
$anchor = "section1";

$url = $base_url . "?" . http_build_query($query_params) . "#" . $anchor;

echo $url;

This will create a URL like http://www.example.com?param1=value1&param2=value2#section1.