Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 21st Aug 2013, 04:22 AM   #1
Warrior Member
 
Join Date: 2013
Posts: 9
Thanks: 4
Thanked 0 Times in 0 Posts
Default
How to handle multiple IF?
Share on: 
fb share twitter share gplus share more share

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?

Keep your dreams alive.
jzeleny is offline  
Unread 21st Aug 2013, 05:55 AM   #2
Designer & Front End Dev
 
Joe Crosbie's Avatar
 
Join Date: 2012
Location: United Kingdom
Posts: 439
Thanks: 136
Thanked 134 Times in 87 Posts
Default
Re: How to handle multiple IF?
Share on: 
fb share twitter share gplus share more share

You'd just use the "elseif" or "else if" statements over and over

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 :)
Joe Crosbie is offline  
Unread 21st Aug 2013, 06:03 AM   #3
Warrior Member
 
Join Date: 2013
Posts: 9
Thanks: 4
Thanked 0 Times in 0 Posts
Default
Re: How to handle multiple IF?
Share on: 
fb share twitter share gplus share more share

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?

Keep your dreams alive.
jzeleny is offline  
Unread 21st Aug 2013, 06:11 AM   #4
Software Engineer
War Room Member
 
wayfarer's Avatar
 
Join Date: 2008
Location: Asheville, NC USA
Posts: 693
Thanks: 37
Thanked 112 Times in 100 Posts
Default
Re: How to handle multiple IF?
Share on: 
fb share twitter share gplus share more share

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.

I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
wayfarer is offline  
The Following User Says Thank You to wayfarer For This Useful Post:
Unread 21st Aug 2013, 11:14 AM   #5
Advanced Warrior
War Room Member
 
Brandon Tanner's Avatar
 
Join Date: 2006
Location: USA
Posts: 1,907
Thanks: 236
Thanked 1,377 Times in 676 Posts
Default
Re: How to handle multiple IF?
Share on: 
fb share twitter share gplus share more share

^ 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.


Brandon Tanner is offline  
Unread 26th Aug 2013, 03:22 PM   #6
Warrior Member
 
Join Date: 2013
Posts: 9
Thanks: 4
Thanked 0 Times in 0 Posts
Default
Re: How to handle multiple IF?
Share on: 
fb share twitter share gplus share more share

Thank you wayfarer, it was exactly what was needed.

Keep your dreams alive.
jzeleny is offline  
Closed Thread


Bookmarks

Tags
handle, multiple, php, statements


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 10:21 PM.