这是我的代码 - 不知道为什么它不起作用:
<?php
$urlroot = $_SERVER['HTTP_HOST'];
$urllink = "http://" . $urlroot;
$DirPath = getcwd() . "'n";
$InnermostDir = basename(rtrim($DirPath, '/'));
if ($InnermostDir == $urlroot) {
$InnermostDir = 'home';
echo $InnermostDir;
};
?>
如果我对$InnermostDir
和$urlroot
进行echo
,它们都显示了域 example.com。所以不知道为什么这不会返回真?
$DirPath
末尾包含一个'n
,该不会被删除,因此字符串不会相等。
rtrim($DirPath, '/')
只会从末尾删除/
字符,而不是'n
。如果您希望它也删除'n
,则需要使用 rtrim($DirPath, "/'n")
,或者在设置$DirPath
时干脆不添加该'n
。
用 trim(( 将两个变量括起来,因此:
<?php
$urlroot = $_SERVER['HTTP_HOST'];
$urllink = "http://" . $urlroot;
$DirPath = getcwd() . "'n";
$InnermostDir = basename(rtrim($DirPath, '/'));
if ( trim($InnermostDir) == trim($urlroot) ) {
$InnermostDir = 'home';
echo $InnermostDir;
};
?>
如果我把它放在我的/var/www 目录中:
<?php
$urlroot = $_SERVER['HTTP_HOST'];
$urllink = "http://" . $urlroot;
$DirPath = getcwd() . "'n";
$InnermostDir = basename(rtrim($DirPath, '/'));
var_dump($InnermostDir);
var_dump($urlroot);
if ($InnermostDir == $urlroot) {
$InnermostDir = 'home';
echo $InnermostDir;
};
?>
并用本地主机/测试调用它.php我得到
字符串(4( "www " 字符串(9( "本地主机">
哪个会失败if
?