gmp_sign

(PHP 4 >= 4.0.4, PHP 5)

gmp_signSign of number

Description

int gmp_sign ( resource $a )

Checks the sign of a number.

Parameters

a

It can be either a GMP number resource, or a numeric string given that it is possible to convert the latter to a number.

Return Values

Returns 1 if a is positive, -1 if a is negative, and 0 if a is zero.

Examples

Example #1 gmp_sign() example

<?php
// positive
echo gmp_sign("500") . "\n";

// negative
echo gmp_sign("-500") . "\n";

// zero
echo gmp_sign("0") . "\n";
?>

The above example will output:

1
-1
0

See Also

Коментарии

Hi !

If you don't have the GMP extension, the sign function is really simple to code.
Here an example of implementation :

<?php
function sign$number ) {
    return ( 
$number ) ? : ( ( $number ) ? -);
}

echo 
sign500 ); // Return 1
echo sign( -500 ); // Return -1
echo sign); // Return 0
?>

Thomas.
2011-10-21 06:11:35
http://php5.kiev.ua/manual/ru/function.gmp-sign.html
Автор:
You can also do this:
<?php
$sign 
min(1max(-1$a));
?>
2014-04-03 13:37:00
http://php5.kiev.ua/manual/ru/function.gmp-sign.html
May be it's bug :
     gmp_sign(1) returns 'false'
2014-09-19 15:00:18
http://php5.kiev.ua/manual/ru/function.gmp-sign.html
Автор:
Another way for those without GMP:

<?php
function sign($number) {
    return 
$number abs($number ?: 1);
}
?>
2014-10-03 18:11:36
http://php5.kiev.ua/manual/ru/function.gmp-sign.html
Автор:
For conciseness or golfing ;)

$s=$n<0|-($n>0)

where $s sign is 1 if positive, -1 if negative, otherwise -0 or +0 which is still 0
2015-11-18 07:04:01
http://php5.kiev.ua/manual/ru/function.gmp-sign.html
Автор:
Using a spaceship in PHP7:

<?php
$sign 
$number <=> 0;
?>
2017-07-18 16:28:31
http://php5.kiev.ua/manual/ru/function.gmp-sign.html

    Поддержать сайт на родительском проекте КГБ