bcmod

(PHP 3, PHP 4, PHP 5)

bcmod --  Get modulus of an arbitrary precision number

Description

string bcmod ( string left_operand, string modulus )

Get the modulus of the left_operand using modulus.

范例

例子 1. bcmod() example

<?php
echo bcmod('4', '2'); // 0
echo bcmod('2', '4'); // 2
?>

参见

bcdiv().


add a note add a note User Contributed Notes
jmullan at visi dot com
25-Jul-2006 06:12
<?php
function bc_is_even($int_str) {
   if (
0 < strlen($int_str)) {
       return !((
substr($int_str, -1)) % 2);
   } else {
      
// error                                                                                               
      
return 0;
   }
}
?>
cristianDOTzuddas]NOSPAM[gmailDOTcom
24-Jul-2005 06:45
BC Math is really cool, but has only 10 functions. So we MUST write other BC functions and share the code.

Here is a simple even/odd number check.

<?
function bc_is_even($int_str) {
   if (
strlen($int_str)>0) {
       if (
bcmod($int_str, 2)==='0')
           return
true;
       else
           return
false;
   }
   else       
// error
      
return 0;
}
?>
lauris at night dot lt
24-Dec-2003 12:04
<?php
/**
 * my_bcmod - get modulus (substitute for bcmod)
 * string my_bcmod ( string left_operand, int modulus )
 * left_operand can be really big, but be carefull with modulus :(
 * by Andrius Baranauskas and Laurynas Butkus :) Vilnius, Lithuania
 **/
function my_bcmod( $x, $y )
{
  
// how many numbers to take at once? carefull not to exceed (int)
  
$take = 5;   
  
$mod = '';

   do
   {
      
$a = (int)$mod.substr( $x, 0, $take );
      
$x = substr( $x, $take );
      
$mod = $a % $y;   
   }
   while (
strlen($x) );

   return (int)
$mod;
}

// example
echo my_bcmod( "7044060001970316212900", 150 );
?>
mackaye at iprimus dot com dot au
17-Sep-2003 01:28
It is correct to use $i%2  to get even and odd program control

HOWEVER

using bit level operators is better, faster and more effecient

so

TO get odd and even  $num&1 is much better

To NULL any element $element^$element is also much better

For New comers
these functions are based on the binary representation of variables in memory.

It so happens that an even number always has a
0 on the end, and an odd number always has a 1 on
the end

so

1010101 &
0000001

is true
timo at in-timo dot de
25-Jun-2003 09:15
Check a 10-digit barcode with the Modulo11-ckecksum like this:

<?php
function modulo11 ($barcode)  {
 
$sum = 0;
  FOR  (
$i=0; $i<=8; $i++ )  {
  
$add = $barcode{$i} * ($i+1);
  
$sum = $sum + $add
  }
 
$modulo = $sum % 11;
  IF (
$modulo == 10 )  { $modulo = "X"; }
  IF (
$barcode{9} == $modulo ) {
   return
true;
  } ELSE {
   return
false;
  }
}
?>

The function returns "true" if the barcode was read properly.