# Reverse a sign - positive number to negative - best way

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
OK, something again that should be easy but Google Searching brings up too
many non related subjects due to terms.

I have a positive number in PHP and I want to change it into a negative.

Therefore 3 to become -3.

How do I do this?

(other then \$value = \$value - \$value - \$value)
I think this works but it does not seem like a correct way to do this
function.

## Re: Reverse a sign - positive number to negative - best way

On Wed, 15 Jul 2009 15:33:05 +0100, <news.freedom2surf.net> wrote:

\$value =  - \$value;

## Re: Reverse a sign - positive number to negative - best way

Hmmm, still not behaving how I expect it to.

I have a function that adds a quantity to a column in Mysql. I wanted to use
the same function  to deduct a value ( why rewrite it, just adapt it a bit)

However when I pass the value to the function

\$value = - \$value;

//Function declation

\$sql = "Update table SET field1= field1 + \$value_to_add";
//run sql
}

Whenever I run this and echo the sql before running it always shows

I'm sure I'm doing something else wrong.

## Re: Reverse a sign - positive number to negative - best way

DOPE! I have found the rror in my code. Thanks.

## Re: Reverse a sign - positive number to negative - best way

On 15 Jul 2009, <news.freedom2surf.net> wrote:

I realize you fixed your error, but just to let you know,
changing the sign of a value will will also cause already
negative values to become positive.  Just to be safe, you might
try something like this:

\$value = -abs(\$value);

The above simply takes the absolute value of a value and makes it
negative.

--
~Curtis Dyer
<?php \$x='<?php \$x=%c%s%c;%cprintf(\$x,39,\$x,39,10,10); ?>%c';
printf(\$x,39,\$x,39,10,10); ?>

## Re: Reverse a sign - positive number to negative - best way

On 15 July, 15:33, <news.freedom2surf.net> wrote:

\$value = -\$value;