php 如果这个页面显示这个否则显示这个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7997016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:44:26  来源:igfitidea点击:

If this Page Show This Else Show This

phpif-statement

提问by Jezthomp

On all pages apart from the contact page, I want it to show the following in the inc-header.php include.

在除联系页面之外的所有页面上,我希望它在 inc-header.php 包含中显示以下内容。

<p><a href="contact.php">Contact</a></p>

On the page contact.php, I want it to show:

在页面上contact.php,我希望它显示:

<p><a href="index.php">Home</a></p>

This should be possible correct?

这应该是可能的吧?

回答by genesis

<?php
if (stripos($_SERVER['REQUEST_URI'], 'contact.php')){
     echo '<p><a href="index.php">Home</a></p>';
}
else{
     echo '<p><a href="contact.php">Contact</a></p>';
}

回答by cweiske

if ($_SERVER["SCRIPT_NAME"] == '/contact.php') {
    echo '<p><a href="index.php">Home</a></p>';
} else {
    echo '<p><a href="contact.php">Contact</a></p>';
}

回答by Marijn van Vliet

There is a global variable named $_SERVER['PHP_SELF']that contains the name of your page currently requested. Combined with basename()this should work:

有一个名为的全局变量$_SERVER['PHP_SELF'],其中包含您当前请求的页面的名称。结合basename()这应该工作:

if( basename($_SERVER['PHP_SELF'], '.php') == 'contact' ) {
    // Contact page
} else {
    // Some other page
}

回答by giorgio

the quick and dirty solution is:

快速而肮脏的解决方案是:

<?php
$current_page = 'contact';
include('inc_header.php');
....
?>

In inc_header.php:

在 inc_header.php 中:

<?php
if($current_page == 'contact') {
    // show home link
} else {
    // show contact link
}
?>

回答by Farray

You can do it with a simple if statement, but this will only work for your contact page. You could also use a simple function in your inc-header file that would work like so:

您可以使用简单的 if 语句来完成,但这仅适用于您的联系页面。您还可以在 inc-header 文件中使用一个简单的函数,其工作方式如下:

function LinkToPageOrHome( $script, $title ){
   if ( strtolower( $_SERVER[ 'SCRIPT_NAME' ] ) == strtolower( $script) ){
       $script = 'home.php';
       $title = 'Home';
   }
   echo '<p><a href="' . $script. '">' . htmlentities( $title ) . '</a></p>';
}

It's sort of a blunt approach from a design standpoint, but you could use LinkToPageOrHome( 'page.php', 'My Page' );in multiple templates and never worry about having a page link to itself.

从设计的角度来看,这是一种生硬的方法,但您可以LinkToPageOrHome( 'page.php', 'My Page' );在多个模板中使用,而不必担心是否有指向自身的页面链接。