|
użytkowników online: 123
|
OPINIE UŻYTKOWNIKÓW
|
W takich dniach, jak ten, nie żałuję, że wykupiłem abonament. Korzystam z porad na tych stronach nawet kilkanaście razy w tygodniu i dzięki nim prace nad stronami dla klientów idą mi o wiele szybciej, a strony wyglądają bardziej profesjonalnie. Nie wiem, jak mogłem wcześniej pracować bez dostępu do porad w tym serwisie!
Wojciech Miszkiewicz
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
XLII. GMP Functions
These functions allow you to work with arbitrary-length integers
using the GNU MP library.
These functions have been added in PHP 4.0.4.
Notatka:
Most GMP functions accept GMP number arguments, defined as
resource below. However, most of these
functions will also accept numeric and string arguments, given
that it is possible to convert the latter to a number. Also,
if there is a faster function that can operate on integer arguments,
it would be used instead of the slower function when the supplied arguments are
integers. This is done transparently, so the bottom line is that
you can use integers in every function that expects GMP
number. See also the gmp_init() function.
| Ostrzeżenie |
If you want to explicitly specify a large integer,
specify it as a string. If you don't do that, PHP will
interpret the integer-literal first, possibly resulting
in loss of precision, even before GMP
comes into play.
|
Notatka:
This extension is available on Windows platforms since PHP 5.1.0.
You can download the GMP library from http://www.swox.com/gmp/. This site also has the
GMP manual available.
You will need GMP version 2 or better to use these functions. Some
functions may require more recent version of the GMP library.
In order to have these functions available, you must compile PHP with
GMP support by using the --with-gmp option.
To rozszerzenie nie definiuje posiada żadnych
dyrektyw konfiguracyjnych w pliku php.ini. To rozszerzenie nie posiada żadnych rodzajów zasobów.
Poniższe stałe są zdefiniowane w tym rozszerzeniu i stają się dostępne, gdy
rozszerzenie jest dokompilowane do PHP, lub załadowane dynamicznie przy starcie.
Przykład 1. Factorial function using GMP |
<?php
function fact($x)
{
$return = 1;
for ($i=2; $i < $x; $i++) {
$return = gmp_mul($return, $i);
}
return $return;
}
echo gmp_strval(fact(1000)) . "\n";
?>
|
|
This will calculate factorial of 1000 (pretty big number)
very fast.
User Contributed Notessabata dot mereeotlhe at gmail dot com
29-Apr-2005 08:43
Appolagies to the previous note
Say you have the binary "text based" in the database:
1000101001
and your change flag is
0000110110
you run the function
change_flag(1, "0000110110", $conn);
your result will be
1000011111
;-)
sabata dot mereeotlhe at gmail dot com
28-Apr-2005 04:44
I did further research and figured out a solution, that i felt was sufficient for the purpose I wanted it for which was, I needed to change a binary representation in a db to signal user overall status within the system for multi statuses eg: check that they have received a comfirmation email or has been banned or rejected and so on whatever you need to signal
PS: the script below is if you using ADOdb with PHP 5.0.4 not tested else where under Windows XP SP2 like in my instance
<<<<<<<<<<=============>>>>>>>>>>>>>
<?php
function change_status($users_id, $change_flag, $conn)
{
$sql = sprintf("SELECT users_status
FROM users where users_id = users_id");
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
$rs = &$conn->Execute($sql);
foreach($rs as $k => $row)
{
$record = $row[0];
}
$xor1 = base_convert($record, 2, 10);
$xor2 = base_convert($change, 2, 10);
$xor3 = ((int)$xor1 ^ (int)$xor2);
$xor4 = sprintf("%b", $xor3);
$sql = sprintf("UPDATE users
SET users_status = '$xor4'
WHERE users_id = $users_id");
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
$rs = &$conn->Execute($sql);
}
change_status(1, "1010101010101", $conn);
?>
==========================
i hope some has the same problem I had and finds this useful...
;-)
sabata dot mereeotlhe at gmail dot com
28-Apr-2005 12:20
Currently I was in search for a solution to do binary exclusive OR operation (Xor) but realized the solution is only in Linux after research, the closest i got to getting a windows solution was this site i managed to find a article by Zilin Du (zilin "at" cs.nyu.edu) hope it helps here is the site : http://www.cs.nyu.edu/exact/core/gmp/
richard-slater.co.uk
22-Feb-2004 10:03
For those (like me) who are trying to do bit masking with very large numbers, here is a useful function to do the work for you.
<?php
function isBitSet($bitMask, $bitMap)
{
return (bool) gmp_intval(gmp_div(gmp_and($bitMask, $bitMap),$bitMask));
}
?>
helvecio_oliveira at yahoo dot com dot br
15-Oct-2003 10:51
=============================================================
A set of very nice functions to handle IP Address with gmplib:
The best way to store a range into a database is store:
dNet ..... decimal representation of a Net
dMask .... decimal representation of a Mask
All another parameters can be calculated.
<?
function f_and($a,$b){
$a=gmp_init(strval($a));
$b=gmp_init(strval($b));
$d=gmp_and($a,$b);
return floatval(gmp_strval($d));
}
function f_or($a,$b){
$a=gmp_init(strval($a));
$b=gmp_init(strval($b));
$d=gmp_or($a,$b);
return floatval(gmp_strval($d));
}
function f_xor($a,$b){
$a=gmp_init(strval($a));
$b=gmp_init(strval($b));
$d=gmp_xor($a,$b);
return floatval(gmp_strval($d));
}
function f_not($a){
$a=gmp_init(strval($a));
$d=gmp_strval($a,2);
$d=str_replace("1","x",$d);
$d=str_replace("0","1",$d);
$d=str_replace("x","0",$d);
$d=gmp_init($d,2);
return floatval(gmp_strval($d,10));
}
function f_dec2bin($a){
$a=gmp_init(strval($a));
return gmp_strval($a,2);
}
function f_bin2dec($a){
$a=gmp_init(strval($a),2);
return floatval(gmp_strval($a,10));
}
function f_ip2dec($a){
$d = 0.0;
$b = explode(".", $a,4);
for ($i = 0; $i < 4; $i++) {
$d *= 256.0;
$d += $b[$i];
};
return $d;
}
function f_dec2ip($a){
$b=array(0,0,0,0);
$c = 16777216.0;
$a += 0.0;
for ($i = 0; $i < 4; $i++) {
$k = (int) ($a / $c);
$a -= $c * $k;
$b[$i]= $k;
$c /=256.0;
};
$d=join('.', $b);
return($d);
}
function f_dec2cidr($a){
$a=gmp_init(strval($a));
$d=strlen(str_replace("0","",gmp_strval($a,2)));
return $d;
}
function f_dec2ipall($dNet,$dMask){
$dWildCard=f_not($dMask);
$IpAll["Net"]=f_dec2ip($dNet);
$IpAll["Mask"]=f_dec2ip($dMask);
$IpAll["WildCard"]=f_dec2ip($dWildCard);
$IpAll["Cidr"]=f_dec2cidr($dMask);
$IpAll["Bcast"]=f_dec2ip(f_or($dNet,$dWildCard));
$IpAll["nIp"]=$dWildCard+1;
$IpAll["nIpUtil"]=$dWildCard-1;
if($IpAll["nIp"] > 2){
$IpAll["IpFrom"]=f_dec2ip($dNet+1);
$IpAll["IpTo"]=f_dec2ip($dNet+$dWildCard-1);
}
else
{
$IpAll["IpFrom"]="-";
$IpAll["IpTo"]="-";
$IpAll["nIpUtil"]=0;
}
return $IpAll;
}
?>
=============================================================
GMP install steps in Mandrake 9.1:
----------------------------------------------------------
cp -r /usr/src/php-devel/extensions/gmp /tmp/gmp
cd /tmp/gmp
phpize
./configure
make install
echo "extension = gmp.so" > /etc/php/90_gmp.ini
Restart apache web server.
----------------------------------------------------------
gmp.so is in:
/usr/lib/php/extensions/
look in phpinfo, the string:
/etc/php/90_gmp.ini
Needs these tools:
autoconf
automake
libtool
m4
php430-devel-430-11mdk.rpm
all rpm
|