PHPWales 2020 - June 3rd to June 4th

## Arithmetic Operators

Remember basic arithmetic from school? These work just like those.

Arithmetic Operators
Example Name Result
+\$a Identity Conversion of \$a to int or float as appropriate.
-\$a Negation Opposite of \$a.
\$a + \$b Addition Sum of \$a and \$b.
\$a - \$b Subtraction Difference of \$a and \$b.
\$a * \$b Multiplication Product of \$a and \$b.
\$a / \$b Division Quotient of \$a and \$b.
\$a % \$b Modulo Remainder of \$a divided by \$b.
\$a ** \$b Exponentiation Result of raising \$a to the \$b'th power. Introduced in PHP 5.6.

The division operator ("/") returns a float value unless the two operands are integers (or strings that get converted to integers) and the numbers are evenly divisible, in which case an integer value will be returned. For integer division, see intdiv().

Operands of modulo are converted to integers (by stripping the decimal part) before processing. For floating-point modulo, see fmod().

The result of the modulo operator % has the same sign as the dividend — that is, the result of \$a % \$b will have the same sign as \$a. For example:

``` <?phpecho (5 % 3)."\n";           // prints 2echo (5 % -3)."\n";          // prints 2echo (-5 % 3)."\n";          // prints -2echo (-5 % -3)."\n";         // prints -2?> ```

### User Contributed Notes 15 notes

37
pr dot dot dot dot k at g dot dot dot com
2 years ago
``` The modulus operator is very poorly suited for such a simple operation as determining if an int is even or odd. On most common systems, modulus performs a division, which is a very slow operation.A much better way to find if a number is even or odd is to use the bitwise & operator.e.g.\$is_odd = \$x & 1; //using and\$is_odd = \$x % 2; //using modulus ```
40
Jonathon Reinhart
13 years ago
``` A very simple yet maybe not obvious use of the modulus (%) operator is to check if an integer is odd or even.<?php  if ((\$a % 2) == 1)  { echo "\$a is odd." ;}  if ((\$a % 2) == 0)  { echo "\$a is even." ;}?>This is nice when you want to make alternating-color rows on a table, or divs.<?php  for (\$i = 1; \$i <= 10; \$i++) {    if((\$i % 2) == 1)  //odd      {echo "<div class=\"dark\">\$i</div>";}    else   //even      {echo "<div class=\"light\">\$i</div>";}   }?> ```
biziclop at vipmail dot hu
5 months ago
``` For positive modulos, here is a mathematically proper modulo which actually works with negative integers.<?php// Inline: \$v is value to be divided, \$m is the modulus\$remainder = ( \$v % \$m + \$m ) % \$m;// Or as a function:function modulo( \$value, \$modulus ){  return ( \$value % \$modulus + \$modulus ) % \$modulus;}// Test:for( \$x = -6; \$x <= 6; ++\$x )  echo \$x, "\t", modulo( \$x, 3 ), "\t<br>\n";/* prints:-6  0-5  1-4  2-3  0-2  1-1  2 0  0 1  1 2  2 3  0*/ ?> ```
16
info at sima-pc dot com
15 years ago
``` Note that operator % (modulus) works just with integers (between -214748348 and 2147483647) while fmod() works with short and large numbers.Modulus with non integer numbers will give unpredictable results. ```
vike2000 at gmail dot com
1 month ago
``` With % (modulo), it looks like php uses the C operator (apart from some sanity-checks), according to following `awk` command, getting the relevant lines of arbitrary commits (dbb462db5d and 04d5086578 for me) in github.com/php/php-src:\$ awk '!F[0]&&/expr '\''%'\'' expr/{p()} !F[0]&&/case ZEND_MOD/{p();F[0]=1;F[1]="return"} !F[0]&&/mod_function\(.*\)/{p();F[0]=2;F[1]="ZVAL_LONG"} '\$(: function p for printing and array F for finding in files follows: )' F[0]&&match(\$0,F[1])&&1==F[0]--{p();delete F;nextfile}function p(){print FILENAME":"FNR":\t"\$0}' Zend/zend_{language_parser.y,{opcode,operators}.c}Zend/zend_language_parser.y:939:        |    expr '%' expr     { \$\$ = zend_ast_create_binary_op(ZEND_MOD, \$1, \$3); }Zend/zend_opcode.c:1042:            case ZEND_MOD:Zend/zend_opcode.c:1043:                return (binary_op_type) mod_function;Zend/zend_operators.c:1333:    ZEND_API int ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2) /* {{{ */Zend/zend_operators.c:1362:        ZVAL_LONG(result, op1_lval % op2_lval); ```
-9
philippe
2 years ago
``` To get a positiv resultfunction modulo(int \$a, int \$b):?int {  if (\$b == 0) {    throw new Exception('modulo : second operand must not be zero');  }  \$b = abs(\$b);  // test \$b == 1 for performance when \$a < 0  return (\$b == 1) ? 0 : ((\$a < 0) ? modulo(\$a + \$b, \$b) : \$a % \$b);} ```
-12
Dominik Buechler
5 years ago
``` In addition to Jonathan's comment, there is a way simpler way to determine if an integer is even or not:<? \$odd = \$i % 2; ?> or<? \$even = !(\$i % 2); ?>This works because a modulo division by 2 will always return either 0 or the rest 1. Since those are valid boolean values you can just invert them by adding a prefixed ! if wanted. ```
-15
arjini at gmail dot com
15 years ago
``` When dealing purely with HTML, especially tables, or other things in "grids"  the modulous operator is really useful for splitting up the data with a seperator.This snippet reads any gif files from the directory the script is in, prints them out and puts in a break every 5th image.<?php    \$d = dir('./');    \$i = 0;    while(false !== (\$e = \$d->read())){        if(strpos(\$e,'.gif')){            ++\$i;            echo '<img src="'.\$e.'"/>'.chr(10);            if(!(\$i%5))                echo '<br/>';        }    }?>For tables just put </tr><tr> in place of the break. ```
-15
TheWanderer
11 years ago
``` It is worth noticing that when working with large numbers, most noticably using the modulo operator, the results depend on your CPU architecture. Therefore, running a decent 64-bit machine will be to your advantage in case you have to perform complex mathematical operations. Here is some example code - you can compare its output on x86 and x86_64 machines:<?php/* tested under PHP 5.2.6-1 with Suhosin-Patch 0.9.6.2 (cli) on both i386 and amd64, Debian lenny/sid */\$a = 2863311530;\$b = 256;\$c = \$a % \$b;echo "\$c <br />\n";echo (2863311530 % 256)." <br />\n"; /* directly with no variables, just to be sure */?>The code is expected to produce '170' if working correctly (try it in spreadsheet software). ```
-25
Andrew
6 years ago
``` The % operator doesn't behave as many people with a maths background would expect, when dealing with negative numbers. For example, -1 mod 8 = 7, but in PHP, -1 % 8 = -1.The following function has the expected behaviour:function mod(\$a, \$n) {    return (\$a % \$n) + (\$a < 0 ? \$n : 0);}mod(-1, 8) returns 7 as expected. ```
-24
lmc at trendicy dot com
5 years ago
``` If you are running a php version older than 5.6, you can calculate \$a ** \$b by using exp(\$b*log(\$a)) ```
-18
peter at icb dot at
2 years ago
``` If you need the mathematical modulo (always positive) from negative numbers, use this small function:<?phpfunction modulo(\$a , \$b) { return (\$a + \$b) % \$b; }// examples:echo modulo(15, 12);  // 3echo modulo(-9, 12);  // 3?> ```
-28
glenn at benge dot co dot nz
15 years ago
``` a real simple method to reset an integer to a the next lowest multiple of a divisor\$startSeq = \$startSeq - (\$startSeq % \$entriesPerPage);if \$startSeq was already a multiple, then " \$startSeq % \$entriesPerPage " will return 0 and \$startSeq will not change. ```
-38
calmarius at atw dot hu
11 years ago
``` Be careful when using % with large numbers.The code:<?php    echo 3333333333 % 3?>puts out -1 instead of zero!(Due to the overflow) ```
-58
php at richardneill dot org
8 years ago
``` For larger numbers (above PHP_INT_MAX), use fmod() rather than %. The other operators (+-*/) work correctly with floats and integer overflow, but % uses integer wrap. Eg. <?php var_dump(0xffffffff % 2); //Prints  int(-1)   which is WRONG var_dump(intval(fmod(0xffffffff,2))); //Prints int(1)   which is the right answer ?> (The reason this matters is that PHP's float is actually a double, and can accurately represent integers up to 52-bits, even on 32-bit systems) ```