How to handle multiple IF?

5 replies
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?
#handle #multiple #php #statements
  • Profile picture of the author Joe Crosbie
    You'd just use the "elseif" or "else if" statements over and over
    Signature
    I chose entrepreneurship over further education despite being laughed at by my friends and family..

    I recently hit the "RESTART" button on my life, read my personal blog to find out how I did it :)
    {{ DiscussionBoard.errors[8421808].message }}
    • Profile picture of the author jzeleny
      Originally Posted by Joe Crosbie View Post

      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?
      {{ DiscussionBoard.errors[8421833].message }}
  • Profile picture of the author wayfarer
    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.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[8421854].message }}
    • Profile picture of the author Brandon Tanner
      ^ 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.
      Signature

      {{ DiscussionBoard.errors[8422828].message }}
  • Profile picture of the author jzeleny
    Thank you wayfarer, it was exactly what was needed.
    {{ DiscussionBoard.errors[8440704].message }}

Trending Topics