Skip to content

Instantly share code, notes, and snippets.

@sanikamal
Created December 18, 2017 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sanikamal/c84017aece704eba1465ed157ce26728 to your computer and use it in GitHub Desktop.
Save sanikamal/c84017aece704eba1465ed157ce26728 to your computer and use it in GitHub Desktop.
PHP arithmetic operators
<!--
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common
arithmetical operations, such as addition, subtraction, multiplication etc.
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
** Exponentiation $x ** $y Result of raising $x to the $y'th power (Introduced in PHP 5.6)
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one
-->
<?php
$x=20;
$y=15;
//addition
$add=$x+$y;
echo $add.'<br>';
//subtraction
$sub=$x-$y;
echo $sub.'<br>';
//multiplication
$mult=$x*$y;
echo $mult.'<br>';
//division
$div=$x/$y;
echo $div.'<br>';
//modulus
$mod=$x%$y;
echo $mod.'<br>';
//Exponentiation
$exp=$x**$y;
echo $exp.'<br>';
$num=20;
$num2=10;
//Pre-increment
echo ++$num.'<br>';
//post-increment
echo $num++.'<br>';
echo $num.'<br>';
//Pre-decrement
echo --$num2.'<br>';
//Post-decrement
echo $num2--.'<br>';
echo $num2.'<br>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment