john
john

Reputation: 409

How to make if / else if into an array?

I have to add noindex to some url's, this is my code at the moment:

<?php if ($metanoindex) { ?>
<meta name="robots" content="noindex,follow" />
<?php } elseif ($_SERVER['REQUEST_URI']=='/dolls/1/bestselling' ) { ?>
<meta name="robots" content="noindex,follow" />
<?php } elseif ($_SERVER['REQUEST_URI']=='/dolls/1/bestselling/30' ) { ?>
<meta name="robots" content="noindex,follow" /> 
<?php } elseif ($_SERVER['REQUEST_URI']=='/dolls/1' ) { ?>
<meta name="robots" content="noindex,follow" /> 
<?php } else { ?>
<meta name="robots" content="index,follow" />
<?php } ?>

I wondered if there is a way to make an array of urls instead of repeating "else if" each time.

Upvotes: 1

Views: 174

Answers (2)

GBD
GBD

Reputation: 15981

Try as below

<?php
    $arr = array('/dolls/1/bestselling','/dolls/1/bestselling/30','/dolls/1');
    if($metanoindex || in_array($_SERVER['REQUEST_URI'],$arr)){ ?>
      <meta name="robots" content="noindex,follow" /> 
<?php
    }
    else {
?>
      <meta name="robots" content="index,follow" />
<?php } ?>

PHP Manual: in_array()

Upvotes: 2

Stanley
Stanley

Reputation: 5127

$urls = array('/dolls/1/bestselling', '/dolls/1/bestselling/30', '/dolls/1');
if (in_array($_SERVER['REQUEST_URI'], $urls)) {
    // Insert meta tag here
}

Upvotes: 1

Related Questions