How to handle multiple IF?

by 5 replies
6
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?
#programming #handle #multiple #php #statements
  • You'd just use the "elseif" or "else if" statements over and over
    • [1] reply
    • Ok, there will be around 30 text strings. Can you write a sample of "else if" repeating sample?
  • 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.
    • [ 1 ] Thanks
    • [1] reply
    • ^ 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.
  • Thank you wayfarer, it was exactly what was needed.

Next Topics on Trending Feed