Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reverse a sign - positive number to negative - best way

11,151 views
Skip to first unread message

news.freedom2surf.net

unread,
Jul 15, 2009, 10:33:05 AM7/15/09
to
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.


Geoff Berrow

unread,
Jul 15, 2009, 11:02:19 AM7/15/09
to

$value = - $value;

Captain Paralytic

unread,
Jul 15, 2009, 11:05:37 AM7/15/09
to

$value = -$value;

news.freedom2surf.net

unread,
Jul 15, 2009, 11:22:06 AM7/15/09
to

"Geoff Berrow" <blth...@ckdog.co.uk> wrote in message

>
> $value = - $value;

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;

myAddFunction ($value)

//Function declation
myAddFunction ($value_to_add) {

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

Whenever I run this and echo the sql before running it always shows
$value_to_add as zero.

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


news.freedom2surf.net

unread,
Jul 15, 2009, 11:38:09 AM7/15/09
to
DOPE! I have found the rror in my code. Thanks.

Curtis Dyer

unread,
Jul 16, 2009, 12:13:12 AM7/16/09
to
On 15 Jul 2009, <news.freedom2surf.net> wrote:

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

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); ?>

0 new messages