www.pickatutorial.com Computer Tutorials

PHP Arithmetic. Free online PHP and MySQL Web Database Programming Tutorial...

Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script
Lessons PHP Arithmetic Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Arithmetic in PHP is very simple. PHP provides various arithmetic operators; for example:
addition +,
subtraction -,
multiplication *,
division /,
modulus %,
increment ++ and
decrement --.

PHP Addition
PHP provides + operator for addition. Consider the following piece of code:

$x = 2; // $x has a value 2
$x = $x + 2; // now $x has a value 4

PHP Subtraction
PHP provides - operator for subtraction. Consider the following piece of code:

$x = 2; // $x has a value 2
$x = $x - 1; // now $x has a value 1

PHP Multiplication
PHP provides * operator for multiplication. Consider the following piece of code:

$x = 2; // $x has a value 2
$x = $x * 3; // now $x has a value 6

PHP Division
PHP provides / operator for subtraction. Consider the following piece of code:

$x = 5; // $x has a value 5
$x = $x / 2; // now $x has a value 2.5

PHP Modulus Operation
PHP provides % operator for modulus operation. Modulus operation is also referred to as division remainder, as it returns the remainder after performing a division. Consider the following piece of code:

$x = 53; // $x has a value 53
$x = $x % 10; // now $x has a value 3

PHP Increment
PHP provides ++ operator for incrementing a number by 1. Consider the following piece of code:

$x = 5; // $x has a value 5
$y = ++$x; // $y has a value 6

PHP Decrement
PHP provides -- operator for decrementing a number by 1. Consider the following piece of code:

$x = 5; // $x has a value 5
$y = --$x; // $y has a value 4

Be careful when using the increments and the decrement operators in expressions. The position of the operators is very important in expressions, --$x means something very different from $x--. When we use

$y = --$x;

the value of x is decremented by 1 and then assigned to the the variable $y. On the other hand if we use

$y = $x--;

the old value of $x is assigned to the variable $y and after the assignment the value of $x is decremented by 1. Same rules apply to the PHP increment operator.
Next: PHP Strings


Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.