dirname($ _ SERVER [“PHP_SELF”])如何正确返回bar?

$path = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$path .=$_SERVER["SERVER_NAME"]. dirname($_SERVER["PHP_SELF"]);

I have this and this is what happens if I make a echo

echo $path
http:://localhost/folder

and without folder

http:://localhost/

if there is a folder not return my bar if I attached a bar as well the result without folder so

$path .=$_SERVER["SERVER_NAME"]. dirname($_SERVER["PHP_SELF"])."/";
echo $path
http:://localhost/folder
http:://localhost//

any idea better or more optimal to get what I want? to give me a single bar in the two cases

If I understand the question correctly, by bar you actually mean / or the forward slash character, and that when the dirname() returns nothing, you end up with // but would like only a single /.

If this is the case, then the following should satisfy what you are looking for.

$path = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://" . $_SERVER["SERVER_NAME"] . '/' . trim(dirname($_SERVER["PHP_SELF"]),'/');

You can use rtrim($path, '/') to trim any / from back of url.