user1021330
user1021330

Reputation: 33

PHP if statements unsure where I'm wrong

I'm trying to specify that my logo appears on some pages and not on others, the only pages I do not want it showing on are the homepage and /index.php. I have the big logo appearing and disappearing as I want it and so I presumed I could just do the opposite for the small logo but I must be doing something wrong. Here is my current code:

<?php
$dunpage = $_SERVER['REQUEST_URI']; if ($dunpage != '/index.php' || $dunpage != '/') {?>      
<h1 class="small-logo">
<a href="/" title="<?php echo $siteName; ?>"><span><?php echo $siteName; ?></a>
</h1>
<?php } 
?>
<?php
$currentpage = $_SERVER['REQUEST_URI']; if ($currentpage == '/' || $currentpage == '/index.php') {?>
<h1 class="logo";>
  <a href="/" title="<?php echo $siteName; ?>"><span><?php echo $siteName; ?></a>
</h1>
<?php } 
?>

Upvotes: 3

Views: 126

Answers (4)

hafichuk
hafichuk

Reputation: 10781

Why don't you use an else?

<?php
$currentpage = $_SERVER['REQUEST_URI']; if ($currentpage == '/' || $currentpage == '/index.php') { ?>
<h1 class="logo";>
  <a href="/" title="<?php echo $siteName; ?>"><span><?php echo $siteName; ?></a>
</h1>
<?php 
} else {
?>
<h1 class="small-logo">
  <a href="/" title="<?php echo $siteName; ?>"><span><?php echo $siteName; ?></a>
</h1>
<?php 
}
?>

(You don't actually mention that it's either a large or a small logo that you want to display, but I have to assume that is the case here.)

Or you can use in_array() in this case.

<?php
$displayMainLogo = array('/', '/index.php');
if ( in_array($_SERVER['REQUEST_URI'], $displayMainLogo) ) { ?>
<h1 class="logo";>
  <a href="/" title="<?php echo $siteName; ?>"><span><?php echo $siteName; ?></a>
</h1>
<?php 
} else {
?>
<h1 class="small-logo">
  <a href="/" title="<?php echo $siteName; ?>"><span><?php echo $siteName; ?></a>
</h1>
<?php 
}
?>

If you understand how to do a ternary (http://php.net/manual/en/language.operators.comparison.php), then this is as terse as you can make it.

<h1 class="<?php echo (in_array($_SERVER['REQUEST_URI'], $displayMainLogo)) ? 'logo' : 'small-logo' ?>";>
  <a href="/" title="<?php echo $siteName; ?>"><span><?php echo $siteName; ?></a>
</h1>

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270687

Instead of a logical OR in your first condition, you should be using a logical AND:

$dunpage = $_SERVER['REQUEST_URI']; if ($dunpage != '/index.php' || $dunpage != '/') {?> 
// Should be
$dunpage = $_SERVER['REQUEST_URI']; if ($dunpage != '/index.php' && $dunpage != '/') {?> 

Effectively, you were saying "act if the page is either not index.php, or not /." So in either of those cases, the opposite would be true. If it wasn't index.php, it could be /, for example.

Upvotes: 3

Dan
Dan

Reputation: 10786

You correctly negated the == operator, but not the || operator. You're saying that if dunpage is not /index.php OR dunpage is not /, do that, which basically means do that always. Change || to && for the small logo.

Upvotes: 2

Alan Moore
Alan Moore

Reputation: 6575

try

if ($dunpage != '/index.php' && $dunpage != '/') {

for your small logo.

Upvotes: 2

Related Questions