Wednesday 26 February 2014

Arithmetic operators in php

Arithmetic Operators in php

This is tutorial about “arithmetic Operators”.Now what you mean by arithmetic Operators or Mathematics Operators? If you know anything about mathematics you will understand that this are +(Addition),-(subtraction),*(Multiplication),/(Division), in php we also use % (Modulus), ++(Increment),–(Decrements).Now you all know about Addition,subtraction,Multiplication and Division. Let’s look at some of its examples:-

Code for Addition (Screenshot1):-

Addition in php
Code for Addition Screenshot1

Output (Screenshot2) :-

arithmetic operators in php
Screenshot2
In this example we are just taking variable 10 and adding it with value 20 and echoing that as variable sum. The answer will be 10 + 20 = 30.

Code for subtraction (Screenshot3):-

operators in php
Screenshot3

Output (Screenshot4):-

Operators php
Screenshot4

In this example we are just taking variable 10 and subtracting it from value 20. The answer will be in negative because we are subtracting small value 10 from bigger 20 and echoing that as variable minus. The answer will be 10 – 20 = -10.

Code for multiplication (Screenshot5):-

multiplication php
Screenshot5

Output (Screenshot6):-

Arithmetic Operators In Php
Screenshot6

In this example we are just taking variables 10 and multiplying it with 20 and echoing that variable as into. The answer will be 10 * 20 = 200.

Code for Division (Screenshot7):-

Arithmetic Operators
Screenshot7

output (Screenshot8):-

operators In php
Screenshot8

In this example we are just taking variables 10 and dividing with 20 and echoing that variable sum. the answer will be 10/20 = 0.5.
So this are basic examples of arithmetic operators. Now in larger programming, we have more than 2 variables. For making long mathematical operation :-

Code for long arithmetic operators (Screenshot9):-

Arithmetic Operators
Screenshot9

output (Screenshot10) :-

Operators In Php
Screenshot10
In this example we had three variables number1,number2 and number3 we are adding number1 with number 2 and dividing with number3. In php,it will first divide number2 from number3 then will add to number1.
Steps in php:-
1.Divide number2(50) / number3(2) = 25.
2.Add number1 + result(25) will echo out 125 which is the standard way for calculation in php. Now what if you want to firstly add variables and then want to divide? You have to give priority to that part which you want to do firstly with brackets. Let see example a quick ex. in it:-

Code for Long arithmetic operators with priority(Screenshot11):-

Code for Long arithmetic operators with priority
Screenshot11

Output (Screenshot12):-

operators
Screenshot12
In this example, firstly it will
1.Add number1(100) + number2(50) =result (150)
2.Divide result(150) / number3 (2) = 75.

Code for Modulus Arithmetic operators(Screenshot13):-

Modules php
Screenshot13

output (Screenshot14) :-

Operators
Screenshot14

In this example number1 divides with number2 and the remainder left is called modulus. In php we use (%) percentage sign, to declare modulus.As you see 10/3 the remainder will be 1.

Code for Increment Arithmetic operators(Screenshot15):-

Code for Increment Arithmetic operators
Code for Increment Arithmetic Operators Screenshot15

output (Screenshot16):-

Arithmetic Operators In php
Screenshot16
In this example we had just incremented number1 with 1.It is incremented and echoed as 11.Now why do we need to create variable increment? The answer is short hand notation of $number + 1 i.e. in short $number1++.

Code for decrements arithmetic operators(Screenshot17) :-

code for Operators
Screenshot17

Output (Screenshot18):-

Php Operators
Screenshot18
In this example we had just decremented number1 with 1. It is decremented and echoed as 9.Now why do we need to create variable decremented? The answer is short hand notation of $number – 1 i.e. in short $number1–.

Now,reason behind “Increment and Decrements  is we do this because in loops, sometimes we need to track how many times the loop had run.This are Standard Operators. It will be useful when you will be programming with database.So, You must have got idea from examples of addition, subtraction, multiplication, division, increment and decrements.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates