Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Calculation with numbers

  1. Aritmetic operations

1. Aritmetic operations

In PHP are 6 simple aritmetic operations. First these are the 4 basic aritmetic operations +, -, * and / (/ is the division as : is already used in php and / is used in any other programming language, too), and there is the unary operator - which negates a number and the operator % which calculate the remains of a division (20:7 = 2 remaining 6).

$b 6;
$a-$b;   // 4
echo $a+$b;   // 16
echo $a*$b;   // 60
echo $a/$b;   // float(1.66666666667)
echo -$a;     // -10, same as echo 0-$a;
echo $a%$b;   // 4 (6 fits one times in 10 with a remain of 4)

For any complex calculaten there exists a lot of functions, to find in the manual at Mathematical Functions. Especially pow is such a function which calculates the exponentation. Beginners try to use ^ as the exponentation and fails as php use ^ for something different than the exponentation (it is used for a bitwise XOR operation).

$exponent 7;
$base^$exponent;        // echos 2, which is not what you want
echo pow($base$exponent);  // echos 78125

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!