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):-
|
Code for Addition Screenshot1 |
Output (Screenshot2) :-
|
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):-
|
Screenshot3 |
Output (Screenshot4):-
|
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):-
|
Screenshot5 |
Output (Screenshot6):-
|
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):-
|
Screenshot7 |
output (Screenshot8):-
|
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):-
|
Screenshot9 |
output (Screenshot10) :-
|
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):-
|
Screenshot11 |
Output (Screenshot12):-
|
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):-
|
Screenshot13 |
output (Screenshot14) :-
|
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 Screenshot15 |
output (Screenshot16):-
|
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) :-
|
Screenshot17 |
Output (Screenshot18):-
|
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..