Jump to content

Repeat statement with elseif?


Novice@PHP

Recommended Posts

I have this code which checks for the word "Big Ben" and if it's not there it outputs some html. I want t be able to make another statement to also check for the word "London Tower" and if it's not there then to check for the word "Canary Wharf",

 

So far I've only managed one statement without the code breaking, how do I add the others in as well.

 

<?php $astacker=get_post_meta($post->ID, 'thesite', true); if ( $astacker == [b]'Big Ben'[/b]) { ?>
<?php include(TEMPLATEPATH."/feedsgrabstack.php");?>
<?php }else { ?>
<div id="splitter"><div class="clear">
<a href="<?php echo get_post_meta($post->ID, "linktosource", true);?>">Click Here To View Answers</a> <span style="float:right;"><a href="#related">See Related Questions</a></div></div>
<?php } ?>

Link to comment
https://forums.phpfreaks.com/topic/225443-repeat-statement-with-elseif/
Share on other sites

an example

 

<?php
$astacker=get_post_meta($post->ID, 'thesite', true);
if ( $astacker == [b]'Big Ben'[/b]) {
     include(TEMPLATEPATH."/feedsgrabstack.php");
} else if ( $astacker == [b]'London Tower'[/b] {
     // Do something else..
} else {
?>
<div id="splitter"><div class="clear">
<a href="<?php echo get_post_meta($post->ID, "linktosource", true);?>">Click Here To View Answers</a> <span style="float:right;"><a href="#related">See Related Questions</a></div></div>
<?php
}
?>

If you want to do the same thing if the variable is any of those, then something like this:

 

$landmarks = array('Big Ben',  'London Tower',  'Canary Wharf');

if(in_array($astacker, $landmarks)) {
   include(TEMPLATEPATH."/feedsgrabstack.php");
   // do something if any of the three match
} else {
  // do default thing
}

 

Might be easier with a switch if you want to do something different depending on the value of the variable:

 

switch($astacker) {

   case 'Big Ben':
      include(TEMPLATEPATH."/feedsgrabstack.php");
      break;

   case 'London Tower':
      // do something
      break;

   case 'Canary Wharf':
      // do something
      break;

   default:
      // do default thing
      break;
}

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.