Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   How to handle multiple IF? (https://www.warriorforum.com/programming/833081-how-handle-multiple-if.html)

jzeleny 21st August 2013 04:22 AM

How to handle multiple IF?
 
Hi, I'm new in PHP thus need small help with his problem:

- this is my current code:
Code:

<?php
 = 'Text string 0 - ultimate info';
 = 'Text string 1';
 = 'Text string 2';
 = 'Text string 3';
 = 'Text string 4';
 = 'Text string 5';

if ([100]) { print ; }
else if ([101]) { print ; }
else { print ; }
?>

I'm PHP newbie and that's why I choose IF statement. But it isn't (probably) enough. I need something like this:

Code:

<?php
 = 'Text string 0 - ultimate info';
 = 'Text string 1';
 = 'Text string 2';
 = 'Text string 3';
 = 'Text string 4';
 = 'Text string 5';
...

if ([100]) { print ; }
if ([101]) { print ; }
if ([102]) { print ; }
if ([103]) { print ; }
if ([104]) { print ; }
...
else { print ; }
?>

May you help me a little bit, please?

Joe Crosbie 21st August 2013 05:55 AM

Re: How to handle multiple IF?
 
You'd just use the "elseif" or "else if" statements over and over :)

jzeleny 21st August 2013 06:03 AM

Re: How to handle multiple IF?
 
Quote:

Originally Posted by Joe Crosbie (Post 8421808)
You'd just use the "elseif" or "else if" statements over and over :)

Ok, there will be around 30 text strings. Can you write a sample of "else if" repeating sample?

wayfarer 21st August 2013 06:11 AM

Re: How to handle multiple IF?
 
You can try using the "switch" statement instead.

PHP Code:

<?php
switch($var) {
    case 
1:
    case 
2:
    case 
3:
        echo 
"something";
        break;
    case 
4:
        echo 
"something else";
        break;
    case 
5:
        echo 
"something else 2";
        break;
    default:
        echo 
"something else 3";
}
?>

Sort of a silly example but you get the idea.

Brandon Tanner 21st August 2013 11:14 AM

Re: How to handle multiple IF?
 
^ Yep. Switch/case is usually best if you have a lot of conditions.

It's hard to tell exactly what you're trying to do with your code though, because the forum strips out a lot of PHP code when you put it inside "CODE" tags. Since you don't have much code... if you post your code without using any of the forum "tags", we'll be able to help you better. Or use something like Pastebin.com, and link to it.

jzeleny 26th August 2013 03:22 PM

Re: How to handle multiple IF?
 
Thank you wayfarer, it was exactly what was needed.


All times are GMT -6. The time now is 08:34 AM.