/
arithmetic.xml
104 lines (102 loc) · 2.91 KB
/
arithmetic.xml
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
<?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>+$a</entry>
<entry>Identity</entry>
<entry>
Conversion of <varname>$a</varname> to <type>int</type> or
<type>float</type> as appropriate.
</entry>
</row>
<row>
<entry>-$a</entry>
<entry>Negation</entry>
<entry>Opposite of <varname>$a</varname>.</entry>
</row>
<row>
<entry>$a + $b</entry>
<entry>Addition</entry>
<entry>Sum of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a - $b</entry>
<entry>Subtraction</entry>
<entry>Difference of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a * $b</entry>
<entry>Multiplication</entry>
<entry>Product of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a / $b</entry>
<entry>Division</entry>
<entry>Quotient of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a % $b</entry>
<entry>Modulo</entry>
<entry>Remainder of <varname>$a</varname> divided by <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a ** $b</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 ("/") 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 <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 <literal>$a % $b</literal>
will have the same sign as <varname>$a</varname>. For example:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo (5 % 3)."\n"; // prints 2
echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2
?>
]]>
</programlisting>
</informalexample>
</para>
<sect2 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="ref.math">Math functions</link></member>
</simplelist>
</para>
</sect2>
</sect1>