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,9952 gold badges17 silver badges15 bronze badges asked May 26, 2011 at 7:44
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
DormouseDormouse 5,0401 gold badge25 silver badges42 bronze badges 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 badges78 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 82k20 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 badges49 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 Source Code
php $num = readline("Enter the number to be checked: \n"); if (! is_numeric($num)) echo "Enter a valid number"; elseif ($num > 0) echo "The entered number $num is positive"; elseif ($num < 0) echo "The entered number $num is negative"; else { echo "The entered number $num is zero"; } ?
How do you check if a number is positive in PHP?php function CheckNumber($x) { if ($x >= 0) { if ($x > 0) $message = "Positive number"; else $message = "Zero"; } else $message = "Negative number"; echo $message.
How do you know if a number is negative?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.. |