PHP Lesson 3: Arithmetic Operators

0
281
PHP Lesson 2: PHP Data Types and Variables
Getting Data from MySql table in php

In PHP Lesson 3: Arithmetic Operators we will be studying the PHP support for mathematical operations. Arithmetic Operators are used to perform certain mathematical operations like Addition, Subtraction, Multiplication, Division, Modulus and so on. PHP also provides support for calculating logarithms, square roots through functions.

Lets perform some basic mathematical operations using PHP Arithmetic Operators.

Consider the below script, which performs all basic 5 arithmetic operations.


<?php

$a = 20;
$b = 5;

//Addition
$sum = $a + $b  //Result will 20 + 5 = 25

//Subtraction
$sub = $a - $b //Result will be 20 - 5 = 15

//Multiplication
$mul = $a * $b //Result will be 20 * 5 = 100

//Division
$div = $a / $b //Result will be 20 / 5 = 4

//Remainder or Modulus
$rem = $a % $b //Result will be 20 % 5 = 0 as there is no remainder out of 20 / 5

?>

In the above example we have used all basic five Arithmetic Operators and you can see these are simple and basic.  The modulus or remainder operator is used to get the remainder of a division operation for example 10 divided by 3 remainder is 1.

In this PHP Lesson 3, we have studies PHP Arithmetic Operators, in the next lesson we will be studying other operators like Assignment Operators, Increment / Decrement Operators, Comparison Operators etc etc.

PHP Lesson 2: PHP Data Types and Variables
Getting Data from MySql table in php

Leave a Reply