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
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
}
?>

Link to comment
Share on other sites

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;
}

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.