Check negative value in php
I was wondering if there is any way to detect if a number is negative in PHP? Show I have the following code:
I need to find out if
jozala 3,9852 gold badges17 silver badges15 bronze badges asked May 26, 2011 at 7:44
BigJobbiesBigJobbies 4,24311 gold badges40 silver badges51 bronze badges 4
Edit: I feel like this was too simple an answer for the rep so here's something that you may also find helpful. In PHP we can find the absolute value of an integer by using the
This would produce answered May 26, 2011 at 7:45
4 I believe this is what you were looking for:
Usage:
Do however note that eval is a dangerous function, therefore use it only if you really, really need to find out if a number is negative. :-) answered Aug 11, 2012 at 0:59
MahnMahn 15.8k16 gold badges60 silver badges77 bronze badges 6
answered May 26, 2011 at 7:52
MartyMarty 38.6k19 gold badges91 silver badges162 bronze badges You could check if
Shakti Singh 81.9k20 gold badges132 silver badges150 bronze badges answered May 26, 2011 at 7:46
answered May 26, 2011 at 7:46
Tudor ConstantinTudor Constantin 25.6k7 gold badges48 silver badges71 bronze badges Don't get me wrong, but you can do this way ;)
Output:
answered Sep 30, 2017 at 23:38
Just multiply the number by -1 and check if the result is positive. answered Oct 22, 2015 at 11:33
1 You could use a ternary operator like this one, to make it a one liner.
answered Jan 23, 2015 at 10:47
1 Can be easily achieved with a ternary operator.
answered Nov 13, 2020 at 6:45
Usman AhmedUsman Ahmed 2,2711 gold badge17 silver badges17 bronze badges 1 I wrote a Helper function for my Laravel project but can be used anywhere.
answered Jul 2, 2021 at 6:57
How do you check if a number is negative in PHP?check number is positive or negative in php. if($number > 0){. echo "the number is positive";. elseif($number < 0){. echo "the number is negative";. echo "the number is zero";. How do you check if a number is positive negative or zero in PHP?php function CheckNumber($x) { if ($x >= 0) { if ($x > 0) $message = "Positive number"; else $message = "Zero"; } else $message = "Negative number"; echo $message. "\n"; } CheckNumber(5.5); CheckNumber(-10.8); ?>
How do you check the number is negative or not?If you're not sure, here is the breakdown:. If a number is greater than zero, it is a positive number.. If a number is less than zero, it is a negative number.. If a number equals to zero, it is zero.. How do I make a negative number positive in PHP?The abs() function is an inbuilt function in PHP which is used to return the absolute (positive) value of a number. The abs() function in PHP is identical to what we call modulus in mathematics. The modulus or absolute value of a negative number is positive.
|