简体   繁体   中英

if file == index.php

I would just like to hear if it is possible to echo something on one page, and not every other page, even though they all get content from another file.

I have 3 pages, and they all have this in code:

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
include_once($path);
?>

If I want "index.php" to echo something that "about.php" does not echo, can you then do it? As the title says, I'd guess on something like this:

if filename == index.php then
echo=hello

I wouldn't make it so specific If you add more and more pages what will you do? Handle them as special cases one by one? Now you have to handle two pages, after three months they'll be 10...and so on.

It's not a perfect solution but as starting point you may use a flag. Beginning like this:

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
$helloWorld = TRUE;
include_once($path);
?>

And in your index.php include file:

if (helloWorld === TRUE) // or if (!!helloWorld)
    echo "Hello world";
if (strpos($_SERVER['SCRIPT_NAME'],'index.php') !== false) {
  echo "do stuff";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM