unknown os platform
Free Beginner PHP Tutorials - Arithmetic operators | Whats-online.info
whats-online-info

Free Beginner PHP Tutorials - Arithmetic operators


Free
2016/08/26 / Science and Tutorials

Learn how to use Arithmetic operators that is " + , - , * , / " in PHP See example:-





Arithmetic operators in PHP


Arithmetic operators take numerical values as their operands and return a single numerical value.

In PHP, Arithmetic Operators perform certain arithmetical operations such as addition, multiplication, subtraction, division, etc.

Some Arithmetic Operators are:

1. Addition (+)
2. Subtraction (-)
3. Multipllication (*)
4. Division (/)
5. Modulus (%)
6. Exponential (**)

For this article, I will discuss how to use arithmetic operators to perform arithmetical operations in PHP.

Note, Arithmetic is performed according to an order of operation (BODMAS)

B - Brackets
O - Of
D - Division
M - Multiplication
A - Addition
S - Subtraction

Addition Operator

Produces the sum of all numeric operands, e.g., floating numbers, integer numbers, double numbers, etc. in PHP as shown below.

Example

<?php $sum = 10 + 10; echo $sum; ?>

The above code snippet will output 20.


Division Operator

Divides one number by another and is used on numeric operands.
Note: an Infinite result is produced if a number is divided by zero.

Example

<?php $devide = 100 / 10; echo $devide; ?>

The above code snippet will output 10.

Multiplication Operator


Example

<?php $multiply = 10 * 10; echo $multiply; ?>

The above code snippet will output 100.

Subtraction Operator

Finds the difference between two numbers.

Example

<?php $subtraction = 100 - 10; echo $subtraction; ?>

The above code snippet will output 90.

Modulus Operator

Finds the remainder after division of one number by another 

Example

<?php $modulus = 103 % 10; echo $modulus; ?>

The above code snippet will output 3.

Exponential Operator

Result of raising $x to the $y'th power 

Example

<?php $x=5; $y=2 $exp = $x ** $y; //Introduced in PHP 5.6 echo $exp; ?>


Also, see other arithmetic functions

Square root ( sqrt() )and power (pow() ) functions

Square root ( sqrt() )


Finds the square root of a numeric operand.
Requires only one parameter.

Example

<?php  $sqrt=sqrt(100); echo $sqrt; ?>

The above code snippet will output 10.

Power ( pow() )


Returns base raised to the power of EXP.
Requires two parameters. The base number and the exponential number (power value)

Example

<?PHP  $pow=pow(10,3); echo $pow; ?>

The above code snippet will output 1000.











You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment