Solved

Positive or Negative value returned from comparison

Posted on 2008-10-01
11
892 Views
Last Modified: 2012-08-13
Hello,
 
 This should be a quick fix for almost anyone :) but i have never had to deal  with absolute value or a comparison of this type before.
 Below is the code i am currently using. Now what i am trying to accomplish is this.
 
 I have a current count and a previous count. Both values are correct coming into the function.
 
 I want to compare the 2 values and find the difference, Either positive or negative or equal etc.
 
 Now if the value is positive i want to output a +(number) if it's negative i want to output -(number)
 
 I thought in theory this would work. However i always seem to evaluate to a positive number.
 
 Perhaps someone can help me out and tell me where i messed up
 
 Thanks for the help.

function FindVariance($Previous,$Current)
		{
				if ( is_numeric($Current) && is_numeric($Previous) ) {
						$Difference = abs( $Current - $Previous );
					}
					
				
					if(Difference > $Current)
					{
						$this->CountChange = "-".$Difference;
					}
					elseif($Difference < $Current )
					{
						$this->CountChange = "+".$Difference;
					}
					else if(!is_numeric($Difference))
					{
						$this->CountChange=null; 
					}
					else
					{
						$this->CountChange = $Difference;
					}
			
		 	return $this->CountChange;
		}

Open in new window

0
Comment
Question by:Defunct-Exodus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
11 Comments
 
LVL 4

Expert Comment

by:Jerrod_W
ID: 22617419
missing a space on the 2nd else if?
0
 
LVL 4

Expert Comment

by:Jerrod_W
ID: 22617423
err, make that the 1st one
0
 

Author Comment

by:Defunct-Exodus
ID: 22617697
Sorry i edited the code a bit before i posted it, The syntax etc appears to be correctly on my end.

But for some reason it never shows a "-" just a "+" even if the value is lower.
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 4

Expert Comment

by:Jerrod_W
ID: 22618474
Can you post your current syntax, cause I'm still seeing errors
0
 

Author Comment

by:Defunct-Exodus
ID: 22618587
Okay here you go,

Hopefully it is something as simple as a syntax error on my part lol.
	function FindVariance($PreviousCount,$CurrentCount)
		{
				if ( is_numeric($CurrentCount) && is_numeric($PreviousCount) ) {
						$Difference = abs( $CurrentCount - $PreviousCount );
					}
					
				
					if($Difference > $CurrentCount)
					{
						$this->CountChange = "-".$Difference;
					}
					elseif($Difference < $CurrentCount)
					{
						$this->CountChange = "+".$Difference;
					}
					else if(!is_numeric($Difference))
					{
						$this->CountChange=null; 
					}
					else
					{
						$this->CountChange = $Difference;
					}
			
		 	return $this->CountChange;
		}

Open in new window

0
 
LVL 4

Expert Comment

by:Jerrod_W
ID: 22619468
As far as I can tell, what I have should work.
       function FindVariance($PreviousCount,$CurrentCount)
                {
                                if ( is_numeric($CurrentCount) && is_numeric($PreviousCount) ) {
                                                $Difference = abs( $CurrentCount - $PreviousCount );
                                        }
                                        
                                
                                        if($Difference > $CurrentCount)
                                        {
                                                $this->CountChange = "-".$Difference;
                                        }
                                        else if($Difference < $CurrentCount)
                                        {
                                                $this->CountChange = "+".$Difference;
                                        }
                                        else if(!is_numeric($Difference))
                                        {
                                                $this->CountChange=null; 
                                        }
                                        else
                                        {
                                                $this->CountChange = $Difference;
                                        }
                        
                        return $this->CountChange;
                }

Open in new window

0
 
LVL 4

Expert Comment

by:Jerrod_W
ID: 22619513
Another quick question. Where are you getting the numbers for previouscount and currentcount? Unless  previouscount is double the value of currentcount, your number will always return positive due to the fact that you are using absolute value.
0
 
LVL 4

Expert Comment

by:Jerrod_W
ID: 22619704

       function FindVariance($PreviousCount,$CurrentCount)
                {
                                if ( is_numeric($CurrentCount) && is_numeric($PreviousCount) ) {
                                                $Difference = abs( $CurrentCount - $PreviousCount );
                                        }
                                        
                                
                                        if($Difference < $CurrentCount)
                                        {
                                                $this->CountChange = "-".$Difference;
                                        }
                                        else if($Difference > $CurrentCount)
                                        {
                                                $this->CountChange = "+".$Difference;
                                        }
                                        else if(!is_numeric($Difference))
                                        {
                                                $this->CountChange=null; 
                                        }
                                        else
                                        {
                                                $this->CountChange = $Difference;
                                        }
                        
                        return $this->CountChange;
                }

Open in new window

0
 
LVL 4

Accepted Solution

by:
Jerrod_W earned 500 total points
ID: 22619836
My php is pretty rusty, but I think will work better towards what you are trying to do. I'm kinda tired, so ignore my other junk.
       function FindVariance($PreviousCount,$CurrentCount)
                {
                                if ( is_numeric($CurrentCount) && is_numeric($PreviousCount) ) {
                                                $Difference = ( $CurrentCount - $PreviousCount );
                                        }
                                        
                                
                                        if($Difference < $CurrentCount)
                                        {
                                                $this->CountChange = "-".$Difference;
                                        }
                                        else if( $Difference  < 0)
                                        {
                                                $this->CountChange = "+".abs ( $Difference );
                                        }
                                        else if(!is_numeric($Difference))
                                        {
                                                $this->CountChange=null; 
                                        }
                                        else
                                        {
                                                $this->CountChange = $Difference;
                                        }
                        
                        return $this->CountChange;
                }

Open in new window

0
 

Author Closing Comment

by:Defunct-Exodus
ID: 31502105
Well that was it, I totally messed up using abs, I should have just tried to keep things simple.

Jerrod, Thank you very much for your help.
0
 
LVL 4

Expert Comment

by:Jerrod_W
ID: 22625175
You may also want to add something to check when the currentcount is less than or equal to zero. You will have some weird things happen if you don't.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question