Jump to content

[SOLVED] if elseif else help


theredking

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

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.