3 Replies - 782 Views - Last Post: 11 September 2008 - 11:25 AM Rate Topic: -----

#1 jakev383  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 27-May 08

Is there an easier way to streamlime variable checks?

Posted 10 September 2008 - 02:00 PM

I have a routine that I need to process - the PHP page calls an external program, and the external program returns 1-6 pieces of data. I'm reading the data in with the split() okay, and the array is read into variables $limit1 through $limit6 but I then need to process the variables depending on their contents. The external program will not always return all 6 variables - it may only return 1 or 2. I'm processing the variables like this right now:
if ($limit1 || $limit2 || $limit3 || $limit4 || $limit5 || $limit6 != "0") {
		if $limit1 == "NO_ONE") {
				$no_one = "1";
		}
		if $limit1 == "NO_TWO") {
				$no_two = "1";
		}
		if $limit1 == "NO_THREE") {
				$no_three = "1";
		}
		if $limit1 == "NO_FOUR") {
				$no_four = "1";
		}
		if $limit1 == "NO_FIVE") {
				$no_five = "1";
		}
		if $limit1 =="NO_SIX") {
				$no_six = "1";
		}
		if $limit2 =="NO_ONE") {
				$no_one = "1";
		}
// and so on
}



I haven't tried it yet, but this should work. I'll then keep processing it to check what the return of $limit2 is, using the same if-then block as above.
My question (finally, right?) is: is there an easier way to process this variable block? I'm not against doing it the long way, but if there's a shorter way I would appreciate any hints.
Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Is there an easier way to streamlime variable checks?

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1469
  • View blog
  • Posts: 8,176
  • Joined: 07-September 06

Re: Is there an easier way to streamlime variable checks?

Posted 10 September 2008 - 03:14 PM

You could put it through a loop so that you just do the checks and update the variable so that you only type the "long" way out once and it loops through the results.

That could look something like so:

$limit = split(":", $returnedValue);
for($i=0; $i<count($limit); $i++){
  // if statements here like so:
  if($limit[$i] == "No_One"){
	// do stuff
  }
  // and so on...
}


Hope that helps.

This post has been edited by BetaWar: 10 September 2008 - 03:16 PM

Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3699
  • View blog
  • Posts: 13,369
  • Joined: 08-August 08

Re: Is there an easier way to streamlime variable checks?

Posted 11 September 2008 - 07:40 AM

You could also use switch/case:
http://www.php.net/switch
Was This Post Helpful? 1
  • +
  • -

#4 jakev383  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 27-May 08

Re: Is there an easier way to streamlime variable checks?

Posted 11 September 2008 - 11:25 AM

Thanks, both of you. I ended up doing it the long way for right now, but both solutions look like a nice way to clean my code later. I'll probably end up using the switch one, since it's something I'm familiar with in bash.
Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1