Home >>PHP Tutorial >PHP Arithmetic Operators

PHP Arithmetic Operators


Arithmetic operators in PHP

PHP supports all standard arithmetic operations, as illustrated by the list of Operators .

Common Arithmetic Operators
Operatos Description
+ Add
- Subtract
* Multiply
/ Divide and return quotient
% Divide and return modulus

Here's example illustrating these operators in action

PHP Arithmetic operators are used to perform mathematical operation on more than one operands.

Some of the standard arithmetic operators are +,-,*,/,%.

The use of some common arithmetic operators is here illustrated by an example as follows:-


Arithmetic operators (+) for addition

<?php
	          
$x=10;
		  
$y=5;
		
//addition
		
$sum=$x+$y;
		
echo "sum=".$sum."<br/>";

?>

Output sum = 15

Arithmetic operators (-) for subtraction

<?php
		
$x=10;
		
$y=5;

//subtraction
$sub=$x-$y;

echo "sub=".$sub."<br/>";

?>

Output sub = 5

Arithmetic operators (*) for multiplication

<?php
               
$x=10;
 
$y=5;
               
//Multiply
$multiply=$x*$y;
		
echo "Multiplication = ".$multiply."<br/>";

?>

Output Multiplication = 50

Arithmetic operators (/) for quotient

<?php
		
$x=10;
		
$y=5;

//quotient
$div=$x/$y;

echo "Div = ".$div."<br/>";

?>

Output Div = 2

Arithmetic operators (%) for remainder

<?php

$x=10;

$y=3;

//remainder
$rem=$x%$y;

echo "remainder=".$rem."<br/>";

?>

Output sub = 0

$x and $y are two integer variables here there are five blocks/modules in this example they are to preform addition, subtraction, multiplication, division and modulus respectively.

$x store the value 10, $y store the value 5. The output of first module is addition of two values 10 and 5 that is 15 ($x+$y=15).

The output of second module is subtraction of two values 10 and 5 that is 5 ($x-$y=5).

The output of third module is multiplication of two values 10 and 5 that is 50 ($x*$y=50).

The output of fourth module is division of two values 10 and 5 that is 2 ($x/$y=2).

The output of last module is modulus of two values 10 and 3 that is 1 ($x%$y=1).


Popular Tutorials