0
$pages = array('Text1.php', 'Text2.php', 'Text3.php');

// $currentpage depends the page you're on

$currentId = array_search($currentpage, $pages);

我有通过 an 访问的 php 脚本,include(Text4.php)并且include(Text4.php)存储在数组中的所有三个 php 脚本中。现在我要做的是设置£currentpage它,使其等于用户当前所在的页面,并匹配一个值,$pages该值应该是当前页面的文件名。

我的问题是,在上面的代码中,应该如何编写 $currentId 变量来执行它应该实现的功能?

4

3 回答 3

1

仅文件名:

$currentPage = basename(__FILE__);

__

目录名+文件名:

$currentPage = __FILE__;

__

仅目录:

$currentPage = __DIR__;
于 2013-01-03T19:55:15.753 回答
0

//如果你想要一个路径

$currentpage = $_SERVER['PHP_SELF'];

//如果你只想要实际的文件名(字面意思是“Test.php”)

$posofslash = strrpos($_SERVER['PHP_SELF'],'/');
$currentpage = substr($_SERVER['PHP_SELF'],$posofslash);
于 2013-01-03T19:47:57.973 回答
0

这是我用于浏览器游戏的一段代码。措辞略有改变,但代码本身运行良好。任何 php 代码都可以放在“if”语句中,我使用了“print”。

    ///---Various Navigation & Functions Depending on Current Page---///

$findpage = strrpos($_SERVER['PHP_SELF'],'/');  
$currentpage = substr($_SERVER['PHP_SELF'],$findpage);
if ($currentpage == '/home.php'){
 print "This message is displayed when the client is on the 'home' page.";
 print "If you so chose, you can put the entire 'home' page code in here.";
 print "While keeping this the only code being executed.";}
if ($currentpage == '/features.php'){
 print "This message is displayed when the client is on the 'features' page.";
 print "If you so chose, you can put the entire 'features' page code in here.";
 print "While keeping this the only code being executed.";}
if ($currentpage == '/menu.php'){
 print "This message is displayed when the client is on the 'menu' page.";
 print "If you so chose, you can put the entire 'menu' page code in here.";
 print "While keeping this the only code being executed.";}
if ($currentpage == '/store.php'){
 print "This message is displayed when the client is on the 'store' page.";
 print "If you so choose, you can put the entire 'store' page code in here.";
 print "While keeping this the only code being executed.";}
于 2015-09-08T06:55:12.337 回答