You are here: Daniel Dumas » Blog, PHP »

PHP Operators : Using Arithmetic Operators

PHP Operators : Using Arithmetic Operators

Using Arithmetic Operators in PHP you can perform mathematical operations on PHP variables. refer to the example below.

Using PHP Arithmetic Operator

//define variables
<?php

$num1 = 50;
$num2 = 30;

//Addition
$sum = $num1 + $num2;

//Subtraction
$difference = $num1 - $num2;

//Multiplication
$product = $num1*$num2;

//Division
$quotient = $num1 / $num2;

//Modulus
$remainder = $num1 % $num2;

?>

you may also use the shorthand using the PHP Assignment Operator and Arithmetic Operator together.

PHP Assignment Operator and Arithmetic Operator Together

//define variables

< ?php

$num1 = 50;
$num2 = 30;

//Addition
$num1 = $num1+ $num2;
//Shorthand for this is..
$num1+=$num2;

//Subtraction
$num1 = $num1 - $num2;
//Shorthand for this is..
$num1-=$num2;

//Multiplication
$num1 = $num1 * $num2;
//Shorthand for this is..
$num1 *=$num2;

//Division
$num1 = $num1 / $num2;
//Shorthand for this is..
$num1 /=$num2;

//Modulus
$num1= $num1 % $num2;
//Shorthand for this is..
$num1%=$num2;

?>

Discussion

  1. That’s Right!…

    This is a really good blog. Good work!…

  2. Thanks!…

    Thanks for all your insight. This site has been really helpful to me….

Leave a Reply





CommentLuv badge