A PHP function that prints the url based on path
I am not sure that $_SERVER['SERVER_NAME'] would be a valuable fallback for $_SERVER['HOST_NAME'] as this can be really any arbitrary string.
Keep your 'protocol' portion of code together.  Also you can streamline conditional for https case.


Example:

$protocol = 'http';
if (!empty($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] === "on") {
    $protocol = 'https';
};


You could also simplify here and just eliminate this https check if you simply build your URL string like //domain.com/uri with no protocol shown.  This would mimic current behavior of simply continuing current protocol.  You might consider adding parameter to force http vs. https if you ever need to move between the two protocols. 


It does not make sense to me why the path of the currently executing script or the current request URI would have anything to do with formulating the URI portion of the URL when this URI value is being explicitly passed in as a parameter to this function.
I do not follow the logic on the null use case for URI.  Why would this case exist?  Is this just to link a page to itself?
You should add validation around parametric input.  In this case, I would guess you need to validate URI as null or non-zero-length string. You might need to validate that that $args is empty string or starts with ?.