1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
<?xml version="1.0" encoding="utf-8"?>
<sect1 xml:id="language.operators.arithmetic">
<title>Arithmetic Operators</title>
<titleabbrev>Arithmetic</titleabbrev>
<simpara>
Remember basic arithmetic from school? These work just
like those.
</simpara>
<table>
<title>Arithmetic Operators</title>
<tgroup cols="3">
<thead>
<row>
<entry>Example</entry>
<entry>Name</entry>
<entry>Result</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>+$a</code></entry>
<entry>Identity</entry>
<entry>
Conversion of <varname>$a</varname> to <type>int</type> or
<type>float</type> as appropriate.
</entry>
</row>
<row>
<entry><code>-$a</code></entry>
<entry>Negation</entry>
<entry>Opposite of <varname>$a</varname>.</entry>
</row>
<row>
<entry><code>$a + $b</code></entry>
<entry>Addition</entry>
<entry>Sum of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry><code>$a - $b</code></entry>
<entry>Subtraction</entry>
<entry>Difference of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry><code>$a * $b</code></entry>
<entry>Multiplication</entry>
<entry>Product of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry><code>$a / $b</code></entry>
<entry>Division</entry>
<entry>Quotient of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry><code>$a % $b</code></entry>
<entry>Modulo</entry>
<entry>Remainder of <varname>$a</varname> divided by <varname>$b</varname>.</entry>
</row>
<row>
<entry><code>$a ** $b</code></entry>
<entry>Exponentiation</entry>
<entry>Result of raising <varname>$a</varname> to the <varname>$b</varname>'th power.</entry>
</row>
</tbody>
</tgroup>
</table>
<simpara>
The division operator <literal>/</literal> returns a <type>float</type>
value unless the two operands are <type>int</type> (or
<link linkend="language.types.numeric-strings">numeric strings</link>
which are type juggled to <type>int</type>) and the numerator is a multiple
of the divisor, in which case an integer value will be returned.
For integer division, see <function>intdiv</function>.
</simpara>
<simpara>
Operands of modulo are converted to <type>int</type>
before processing. For floating-point modulo, see
<function>fmod</function>.
</simpara>
<para>
The result of the modulo operator <literal>%</literal> has the same sign
as the dividend — that is, the result of <code>$a % $b</code>
will have the same sign as <varname>$a</varname>. For example:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
var_dump(5 % 3);
var_dump(5 % -3);
var_dump(-5 % 3);
var_dump(-5 % -3);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(2)
int(2)
int(-2)
int(-2)
]]>
</screen>
</informalexample>
</para>
<sect2 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="ref.math">Math functions</link></member>
</simplelist>
</para>
</sect2>
</sect1>
|