mirror of
https://github.com/php/doc-en.git
synced 2026-03-23 23:32:18 +01:00
115 lines
3.1 KiB
XML
115 lines
3.1 KiB
XML
<?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:
|
|
<example>
|
|
<title>The Modulo Operator</title>
|
|
<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>
|
|
</example>
|
|
</para>
|
|
<sect2 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><link linkend="ref.math">Math functions</link></member>
|
|
</simplelist>
|
|
</para>
|
|
</sect2>
|
|
</sect1>
|