13 Replies - 673 Views - Last Post: 16 June 2010 - 07:22 PM Rate Topic: -----

#1 xmodz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 38
  • Joined: 07-March 10

comparison dilemma

Posted 15 June 2010 - 03:04 AM

Hey guys, it's me again and this time i have a bit of trouble with comparing almost similar value, here's my code :

		if(($dataDep < $dataKred) || ($dataDep == 0)){
			$this->kreditLogic();
		}
		if(($dataKred < $dataDep) || ($dataKred == 0)){
			$this->depositLogic();
		}
		if(isset($dataDep)){
		       if($dataDep != 0){
				if(isset($dataKred)){
					if($dataKred != 0){
						if(($dataKred) === ($dataDep)){ <-- this is he prob
							$this->b2bLogic();
						}
					}
				}
			}
		}
	}


I wanted b2bLogic to execute when $dataKred is within range of perhaps in 5 value to $dataDep. I hope someone can shed some light on this. many thanks in advance
:helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: comparison dilemma

#2 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: comparison dilemma

Posted 15 June 2010 - 03:07 AM

Do you mean something like this:

if ($dataKred >= ($dataDep - 5) && $dataKred <= ($dataDep + 5)) {

}



?
Was This Post Helpful? 2
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: comparison dilemma

Posted 15 June 2010 - 03:08 AM

Where did you get that syntax for the if statement?

I would just use :
if($dataKred == $dataDep)


Was This Post Helpful? 0
  • +
  • -

#4 xmodz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 38
  • Joined: 07-March 10

Re: comparison dilemma

Posted 15 June 2010 - 03:14 AM

@mahcuz

Thanks Bro that's exactly what I was trying to figure out ~.^

@no2pencil

well I was trying to execute a function b2bLogic when $dataKred is within range of 5 values however I'm still too green to figure out the proper trigger :blush:
Was This Post Helpful? 0
  • +
  • -

#5 xmodz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 38
  • Joined: 07-March 10

Re: comparison dilemma

Posted 15 June 2010 - 03:29 AM

1 more thing tho, how do you silence a function (prevent it from executing) even when the conditions are satisfied?
Was This Post Helpful? 0
  • +
  • -

#6 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: comparison dilemma

Posted 15 June 2010 - 05:41 AM

View Postxmodz, on 15 June 2010 - 09:29 AM, said:

1 more thing tho, how do you silence a function (prevent it from executing) even when the conditions are satisfied?


I'm not sure I understand. Can you give me an example?
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3030
  • View blog
  • Posts: 10,553
  • Joined: 08-August 08

Re: comparison dilemma

Posted 15 June 2010 - 09:26 AM

if ( abs($dataKred - $dataDep) <= 5) {
  $this->b2bLogic();
}



If there is some case where you don't want it called even if the values are within 5 of each other, you need to include that in the if statement. This will not call the function if $othercase is true.

if ( abs($dataKred - $dataDep) <= 5 && !$othercase) {
  $this->b2bLogic();
}


Was This Post Helpful? 3
  • +
  • -

#8 xmodz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 38
  • Joined: 07-March 10

Re: comparison dilemma

Posted 15 June 2010 - 06:36 PM

View Postmahcuz, on 15 June 2010 - 04:41 AM, said:

I'm not sure I understand. Can you give me an example?


I was thinking that even with the fix you suggested the logic might still be flawed due to the other 2 conditions as I was actually trying to get only 1 of those 3 functions to execute
		if(($dataDep < $dataKred) || ($dataDep == 0)){ <-- gotta prevent this
			$this->kreditLogic();
		}
		if(($dataKred < $dataDep) || ($dataKred == 0)){ <-- and this from executing
			$this->depositLogic();
		}
		if(isset($dataDep)){ <-- if this is TRUE
			if($dataDep != 0){
				if(isset($dataKred)){
					if($dataKred != 0){
						if ($dataKred >= ($dataDep - 2) && $dataKred <= ($dataDep + 2)){
							$this->b2bLogic();							
						}
					}
				}
			}
		}
	}

thanks for the reply anyway :)

View PostCTphpnwb, on 15 June 2010 - 08:26 AM, said:

if ( abs($dataKred - $dataDep) <= 5) {
  $this->b2bLogic();
}



If there is some case where you don't want it called even if the values are within 5 of each other, you need to include that in the if statement. This will not call the function if $othercase is true.

if ( abs($dataKred - $dataDep) <= 5 && !$othercase) {
  $this->b2bLogic();
}



I'll try you suggestion mate, Thanks a lot :)
Was This Post Helpful? 0
  • +
  • -

#9 xmodz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 38
  • Joined: 07-March 10

Re: comparison dilemma

Posted 15 June 2010 - 08:16 PM

Problem solved...

here's the finished code
		$modifier = ($dataKred >= ($dataDep - 1) && $dataKred <= ($dataDep + 1));
		
		if(($dataDep < $dataKred) && ($dataDep == 0)){
			$this->kreditLogic();
			}
			elseif(($dataDep < $dataKred) && !$modifier){
				$this->kreditLogic();
				}
		if(($dataKred < $dataDep) && ($dataKred == 0)){
			$this->depositLogic();
			}
			elseif(($dataKred < $dataDep) && !$modifier){
				$this->depositLogic();
				}
		if(isset($dataDep)){
			if($dataDep != 0){
				if(isset($dataKred)){
					if($dataKred != 0){
						if ($dataKred >= ($dataDep - 1) && $dataKred <= ($dataDep + 1)){
							$this->b2bLogic();							
						}
					}
				}
			}
		}
	}


Thanks mahcuz and CTphpnwb ++ for you two :clap: ^^
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3030
  • View blog
  • Posts: 10,553
  • Joined: 08-August 08

Re: comparison dilemma

Posted 16 June 2010 - 06:39 AM

You didn't use: abs($dataKred - $dataDep)
You should, because all you're really looking for is the difference between the two points, and that is always the absolute value of one minus the other: |a-b|
Also, this:
                if(isset($dataDep)){
                        if($dataDep != 0){


is much better this way:
                if(isset($dataDep) && $dataDep != 0)){


or this way:
                if(isset($dataDep) && $dataDep)){


so your nested if statements could read like this:

if(isset($dataDep) && $dataDep && isset($dataKred) && $dataKred)){



That is much easier to read and edit later on if you need to make changes. Always think about what it will be like if you have to come back to your code 6 months or a year after you've written it. Will you be able to remember/read it?

This post has been edited by CTphpnwb: 16 June 2010 - 06:44 AM

Was This Post Helpful? 1
  • +
  • -

#11 xmodz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 38
  • Joined: 07-March 10

Re: comparison dilemma

Posted 16 June 2010 - 06:46 PM

View PostCTphpnwb, on 16 June 2010 - 05:39 AM, said:

You didn't use: abs($dataKred - $dataDep)
You should, because all you're really looking for is the difference between the two points, and that is always the absolute value of one minus the other: |a-b|
Also, this:
                if(isset($dataDep)){
                        if($dataDep != 0){


is much better this way:
                if(isset($dataDep) && $dataDep != 0)){


or this way:
                if(isset($dataDep) && $dataDep)){


so your nested if statements could read like this:

if(isset($dataDep) && $dataDep && isset($dataKred) && $dataKred)){



That is much easier to read and edit later on if you need to make changes. Always think about what it will be like if you have to come back to your code 6 months or a year after you've written it. Will you be able to remember/read it?


Thanks for the suggestion mate, imma improvise my code further :genius:
Was This Post Helpful? 0
  • +
  • -

#12 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: comparison dilemma

Posted 16 June 2010 - 07:14 PM

Really, if you're doing if(isset($dataDep) && $dataDep), the isset() check becomes redundant. if($dataDep) would serve the same purpose, because for if($dataDep) to be satisfied, $dataDep would have to be set to begin with ;)
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3030
  • View blog
  • Posts: 10,553
  • Joined: 08-August 08

Re: comparison dilemma

Posted 16 June 2010 - 07:19 PM

Yes, but if $dataDep isn't set, then if($dataDep) will generate a warning notice in the error log file for most php setups.
Was This Post Helpful? 0
  • +
  • -

#14 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: comparison dilemma

Posted 16 June 2010 - 07:22 PM

Yeah, you're right. I'm just used to working under E_ALL ^ E_NOTICE ^ E_DEPRECATED :)

This post has been edited by Valek: 16 June 2010 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1