TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-09-2009, 08:31 PM   #1 (permalink)
The Visitor
 
Join Date: Feb 2009
Posts: 4
Thanks: 0
xtsu is on a distinguished road
Default Nested if's or is there a better way?

Hey Guys and Gals,

I know when I have written something for PHP there is a million ways to do the same thing. My question is what is the best way with if's and elseif's?

What I want to do is I have 4 variables to compare. If variable 1 is less than 30 then do this that is fairly simple. However, the issue becomes harder when I have to compare all 4 variables at once. Var1 could be equal to 8- but var 2, 3 and 4 are all less than 400 so do this. or 2 and 4 could be greater than 400 but 3 less than, then do this.

The way I do it now is this

Code:
if($row[2]>='80' && $data[1]<'300' && $data[4]>='400' && $data[6]>=400)
I know that I could also do it another way like this:

Code:
if($row[2]>='80'){
     if ($data[1]<'300') {
          if ($data[4]>='400') {
               if ($data[6]>=400){
               }
           }
      }
}
I am just not sure which would be easier, as well there might be a different way to do this. The first way seems to be too specific and I might miss some cases. The second way would be 100% sure not to miss any cases but seems to be way more coding than I need as well not the right way to do it.

Suggestions?
xtsu is offline  
Reply With Quote
Old 02-09-2009, 09:00 PM   #2 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

I'd definetely prefer the first one, because the code is easier to understand, simpler - and looks just nicer.
Sakakuchi is offline  
Reply With Quote
Old 02-09-2009, 10:30 PM   #3 (permalink)
The Visitor
 
Join Date: Feb 2009
Posts: 4
Thanks: 0
xtsu is on a distinguished road
Default

I do as well, I just don't want to miss any cases.
xtsu is offline  
Reply With Quote
Old 02-09-2009, 10:57 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Yes. The first way, I suppose. Although lay it out a little better by adding more spaces:

php Code:
if ($row[2] >= 8 && $data[1] < 300 && $data[4] >= 400 && $data[6] >= 400)
{
    /* Do something... */
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested While Loop Issue Jako General 3 08-06-2008 06:13 AM
having nested mysql_fetch_assoc statements sarmenhb Absolute Beginners 5 06-13-2008 04:12 PM
Horizontal nested loop problem Dracula General 3 06-05-2008 03:12 PM
Need help on nested loop! pabin Absolute Beginners 4 05-12-2008 10:27 AM
PHP Script to print nested array sunilbhatia79 Script Giveaway 2 11-16-2007 06:43 PM


All times are GMT. The time now is 08:51 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design