Positive or Negative value returned from comparison

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

Defunct-ExodusAsked:
Who is Participating?
 
Jerrod_WCommented:
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
 
Jerrod_WCommented:
missing a space on the 2nd else if?
0
 
Jerrod_WCommented:
err, make that the 1st one
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Defunct-ExodusAuthor Commented:
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
 
Jerrod_WCommented:
Can you post your current syntax, cause I'm still seeing errors
0
 
Defunct-ExodusAuthor Commented:
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
 
Jerrod_WCommented:
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
 
Jerrod_WCommented:
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
 
Jerrod_WCommented:

       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
 
Defunct-ExodusAuthor Commented:
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
 
Jerrod_WCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.