Jump to content

Recommended Posts

I'm desperately trying to grasp this simple concept... I can make it work in its simplest forms, but as I try to get more complicated things start to go wrong.

 

If I have a basic if, elseif, else statement:

 

if () {

do something

}

 

elseif () {

do something else

}

 

else () {

do another thing

}

 

Can I add additional "ifs" inside of this? I want to have multiple ifs to echo my form results based on what is inputted into the form. So, if one thing is inputted, I want to echo certain results, but then instead of an elseif something else, I want to echo something else as well "if" something else is inputted or selected.

 

I'm sure this doesn't make too much sense... I'd post my code, but it's a fair bit of a mess, and would take quite a long time to explain.

 

Is there a great if elseif tutorial anyone could point me to?

 

Link to comment
https://forums.phpfreaks.com/topic/68071-solved-if-elseif-else-help/
Share on other sites

You can nest conditionals.

 

<?php
if(condition) {
do this;
      if(some other condition) {
      do this if condition and some other condition is true;
      }
} elseif (some other condition) {
do this;
} else {
fall back on this;
?>
}

 

You can also use a shorter form of conditionals

(condition) ? true : false

 

So, for example say you have this. (This is a bad example)

<?php
$rose = 'red';

if($rose) { //check to see if $rose is true
echo 'The rose is '. ($rose == red) ? 'red' : 'blue';
} ?>

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.