Stopping at "zero".

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Stopping at "zero".

Post by Mightywayne »

So I've got this script. Takes the info from the database fine, and now I've got to substract things.. err, example.

Code: Select all

$value1 = 5
$value2 = 9

$value3 = $value1 - $value2
So what happens is, the script will actually make it go into -4. >_> But I need it to stop at zero. I'm sure/hoping it's a simple answer.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Is this in a loop of some sort? If so please post your code with the loop logic, or a sample loop.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Argh, you posted just as I found it. >_>; Oh boy.

abs()

I'm pretty sure that's it. I didn't go to my math class this year (long story) so I've pretty much forgotten absolute numbers turn negatives to positives. let's see if it works.

Edit: No. I suck at math. ... It just returned a positive number that wasn't zero.

And well yes, this IS a loop... but uhm. It's quite long and it's really not that important, because I tried it on its own and I still get the error.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

$value3 = ($value3 < 0) ? 0 : $value3;
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Aha! It's stupid small posts like these that make me wonder if I should just delete the topic.

But I suppose it'd be frowned upon because of not helping or something.

REGARDLESS! Thank you gentlemen both. I didn't even know of the ? thing.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$foo = max(0, $one - $two);
Post Reply