
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP - Arithmetic Operators Examples
In PHP, arithmetic operators are used to perform mathematical operations on numeric values. The following table highlights the arithmetic operators that are supported by PHP. Assume variable "$a" holds 42 and variable "$b" holds 20 −
Operator | Description | Example |
---|---|---|
+ | Adds two operands | $a + $b = 62 |
- | Subtracts the second operand from the first | $a - $b = 22 |
* | Multiply both the operands | $a * $b = 840 |
/ | Divide the numerator by the denominator | $a / $b = 2.1 |
% | Modulus Operator and remainder of after an integer division | $a % $b = 2 |
++ | Increment operator, increases integer value by one | $a ++ = 43 |
-- | Decrement operator, decreases integer value by one | $a -- = 42 |
Basic Usage of Arithmetic Operators
The following example shows how you can use these arithmetic operators in PHP −
<?php $a = 42; $b = 20; $c = $a + $b; echo "Addition Operation Result: $c \n"; $c = $a - $b; echo "Subtraction Operation Result: $c \n"; $c = $a * $b; echo "Multiplication Operation Result: $c \n"; $c = $a / $b; echo "Division Operation Result: $c \n"; $c = $a % $b; echo "Modulus Operation Result: $c \n"; $c = $a++; echo "Increment Operation Result: $c \n"; $c = $a--; echo "Decrement Operation Result: $c"; ?>
Output
It will produce the following output −
Addition Operation Result: 62 Subtraction Operation Result: 22 Multiplication Operation Result: 840 Division Operation Result: 2.1 Modulus Operation Result: 2 Increment Operation Result: 42 Decrement Operation Result: 43
Arithmetic Operations with Negative Numbers
The below PHP program performs basic mathematical operations using two numbers in which one number is negative number. So see how the arithmetic operators perform.
<?php $x = -10; $y = 5; echo "Addition: " . ($x + $y) . "\n"; echo "Subtraction: " . ($x - $y) . "\n"; echo "Multiplication: " . ($x * $y) . "\n"; echo "Division: " . ($x / $y) . "\n"; echo "Modulus: " . ($x % $y) . "\n"; ?>
Output
It will generate the following output −
Addition: -5 Subtraction: -15 Multiplication: -50 Division: -2 Modulus: 0
Arithmetic Operations with Floating-Point Numbers
This PHP the program performs basic math with decimal numbers. It can do addition, subtraction, multiplicationand division operations on two numbers. The results are displayed using the echo statement.
<?php $a = 5.5; $b = 2.2; echo "Addition: " . ($a + $b) . "\n"; echo "Subtraction: " . ($a - $b) . "\n"; echo "Multiplication: " . ($a * $b) . "\n"; echo "Division: " . ($a / $b) . "\n"; ?>
Output
This will generate the below output −
Addition: 7.7 Subtraction: 3.3 Multiplication: 12.1 Division: 2.5
Increment and Decrement Operators
Now the below code uses the arithmetic operators to show how the Increment and Decrement operations can be performed.
<?php $count = 10; echo "Original value: " . $count . "\n"; echo "After Increment: " . $count++ . "\n"; echo "After After Increment: " . $count . "\n"; echo "Before Increment: " . ++$count . "\n"; echo "Post-Decrement: " . $count-- . "\n"; echo "After Post-Decrement: " . $count . "\n"; echo "Pre-Decrement: " . --$count . "\n"; ?>
Output
This will create the below output −
Original value: 10 After Increment: 10 After After Increment: 11 Before Increment: 12 Post-Decrement: 12 After Post-Decrement: 11 Pre-Decrement: 10