What is the best practice for retrieving the domain name under which a PHP script is running?

To retrieve the domain name under which a PHP script is running, you can use the $_SERVER['HTTP_HOST'] variable. This variable contains the domain name of the server that is hosting the current script. By accessing this variable, you can easily retrieve the domain name and use it in your PHP script for various purposes.

$domain = $_SERVER['HTTP_HOST'];
echo $domain;