![]() |
|
PHP Arithmetic. Free online PHP and MySQL Web Database Programming Tutorial... |
| Lessons | PHP Arithmetic |
|
|
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
|