Negating Variables

by Aug 30, 2013

Let's assume you have a variable with a negative value, and you'd like to make it a positive value. Here's a simple way:

Basically, this approach negates the variable, so you can do the opposite as well:

Note that this will not change $a unless you reassign the result to the variable.

If you just want to make sure a variable has a positive number, try Abs():

To make sure your variable is always negative, you can combine both:

Twitter This Tip! ReTweet this Tip!