Core PHP

PHP Lesson 7: Numerical Operations

Overview

This lesson explains how various numerical operations are applied in PHP. These operations include the basic arithmetic operators, the assignment operators, the increment and decrement operators, and the random number generator.

Basic Arithmetic Operators

The basic aritmetic operators are addition, subtraction, multiplication, and division. Since there is an exponentiation operator in PHP, that has been included here as well; it is designated with a double asterisk: **. Division is somewhat more complicated, because we can perfom ordinary calculator division with a decimal result, or we can perform the division algorithm with a quotient and a remainder. PHP incorporates weak data typing to further compliate the matter.

To perform ordinary decimal division, we can use the division operator: /. However, when the division operator is applied to two integers that do not divide evenly, say 9 and 4, the result is a 2.25, which is not an integer. To get the integer quotient 2, we need to call the intdiv() function. To get the remainder, we use the modulus operator: %. However, when the modulus operator is applied to floating-point numbers, they are first converted to integers via truncation before the modulus operator is applied. So, if the modulus operator is applied to 3.2 and 1.5, they first converted to 3 and 1 and the result is 0, since 1 divides 3 evenly. If we want to get the division remainder of .2, we need to use call the fmod() function as this program illustrates.

Prog1.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$fX = 3.52;
$fY = 2.7;

echo "<pre>";
echo '$fX + $fY = '."$fX + $fY = ".($fX + $fY)."<br />";
echo '$fX - $fY = '."$fX - $fY = ".($fX - $fY)."<br />";
echo '$fX * $fY = '."$fX * $fY = ".($fX * $fY)."<br />";
echo '$fX ** $fY = '."$fX ** $fY = ".($fX ** $fY)."<br /><br />";

echo "Floating-point division operations:<br />";
echo '$fX / $fY = '."$fX / $fY = ".($fX / $fY)."<br />";
echo 'fmod($fX, $fY) = '."fmod($fX, $fY) = ".fmod($fX, $fY)."<br /><br />";

$iA = 34;
$iD = 7;
echo "Integer division operations:<br />";
echo 'intdiv($iA, $iD) = '."intdiv($iA, $iD) = ".intdiv($iA, $iD)."<br />";
echo '$iA % $iD = '."$iA % $iD = ".($iA % $iD)."<br />";
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog1.php

 

Assignment Operators

Assignment operators are designated by combining aritmetic operators an the equals character: +=, -=, *=, /=, %=, and **=. The assignment operators create a convenient shorthand by combining an arithmetic operation and an assignment. So, the operation x += y is a shorter way of writing x = x + y.

Prog2.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$fX = 3.52;
$fY = 2.7;

echo "<pre>";
echo '$fX += $fY => '."$fX += $fY   =>   ".'$fX = '.($fX += $fY)."<br />";
echo '$fX -= $fY => '."$fX -= $fY   =>   ".'$fX = '.($fX -= $fY)."<br />";
echo '$fX *= $fY => '."$fX *= $fY   =>   ".'$fX = '.($fX *= $fY)."<br />";
echo '$fX /= $fY => '."$fX /= $fY   =>   ".'$fX = '.($fX /= $fY)."<br />";
echo '$fX **= $fY => '."$fX **= $fY   =>   ".'$fX = '.($fX **= $fY)."<br /><br />";

$iA = 32;
$iD = 7;
echo '$iA %= $iD => '."$iA %= $iD   =>   ".'$iA = '.($iA %= $iD)."<br />";
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog2.php

 

Increment and Decrement Operators

The increment and decrement operators are designated with a double plus sign or a double minus sign: ++ or --. So, the x++ is a shorter way of writing x = x + 1, and x-- is a shortter way of writing x = x - 1. To complicate the matter somewhat, both operators can be written in prefix and postfix notation. When we write the operator before the variable, ++x, it is called prefix notation. When we write the operator after the variable, x++, it is called prefix notation.

The prefix and postfix notations act slightly different. Suppose that x is 3. When we evaluate, ++x, its value is 4. When we evaluate x++, its value is 3. Both operators will, however, change the value of x to 4. The only difference occurs when evaluating the expressions ++x and x++. In the first case, the prefix expression is the value after the increment, while the postfix is the value before the increment. The decrement operator acts similarly.

Prog3.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
echo "<pre>";
$iA = 1;
echo '$iA = '.$iA.'  =>   ++$iA = '.(++$iA).'   =>   $iA = '.$iA.'<br />';
$iA = 1;
echo '$iA = '.$iA.'  =>   $iA++ = '.($iA++).'   =>   $iA = '.$iA.'<br />';
$iA = 1;
echo '$iA = '.$iA.'  =>   --$iA = '.(--$iA).'   =>   $iA = '.$iA.'<br />';
$iA = 1;
echo '$iA = '.$iA.'  =>   $iA-- = '.($iA--).'   =>   $iA = '.$iA.'<br />';
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog3.php

 

Random Number Generators

Generating a random number is somewhat different operation, but it is common in computer programming. It should be said that computer do not generate random numbers, rather they generated numbers that appeaar to be random; for this reason, they are often called pseudorandom numbers. In fact, the numbers that are generated are exactly the same if we begin at the same start value, called the seed value.

The seed value is set by either the srand() or mt_srand() function. Once the seed is set, we can generate pseudorandom integer values in the range from A to B by passing those values as arguments into the rand() or mt_rand() function. The largest value that can be generated is given by calling either the getrandmax() or mt_getrandmax() function.

Note that functions srand() and mt_srand() are exactly the same. Likewise, rand() and mt_rand() are exactly the same. Furthermore, the functions getrandmax() or mt_getrandmax() are exactly the same. These names come from a time when rand() and mt_rand() used different algorithms to generate numbers. Now they are use the same one. So, there is no difference between them.

Prog4.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
echo "<pre>";
echo "Randomization functions:<br />";
echo "mt_srand(0) ".mt_srand(0)."<br />";
echo "mt_rand(0, 1000) = ".mt_rand(0, 1000)."<br />";
echo "mt_getrandmax() = ".mt_getrandmax()."<br />";
echo "mt_srand() ".mt_srand()."<br />";
echo "mt_rand() = ".mt_rand()."<br /><br />";

echo "Second call:<br />";
echo "mt_srand(0) ".mt_srand(0)."<br />";
echo "mt_rand(0, 1000) = ".mt_rand(0, 1000)."<br />";
echo "mt_getrandmax() = ".mt_getrandmax()."<br />";
echo "mt_srand() ".mt_srand()."<br />";
echo "mt_rand() = ".mt_rand()."<br /><br />";

echo "Old function names:<br />";
echo "srand(0) ".srand(0)."<br />";
echo "rand(0, 1000) = ".rand(0, 1000)."<br />";
echo "getrandmax() = ".getrandmax()."<br />";
echo "srand() ".srand()."<br />";
echo "rand() = ".rand()."<br /><br />";

echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog4.php

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.