Using Arithmetic Operators
To perform mathematical operations on variables, use the standard arithmetic
operators, as illustrated in the following example:
To perform an arithmetic operation simultaneously with an assignment, use the
two operators together. The following two code snippets are equivalent:
<?php
$a = $a + 10;
?>
<?php
$a += 10;
?>
To perform mathematical operations on variables, use the standard arithmetic
operators, as illustrated in the following example:
<?php // define variables $num1 = 101; $num2 = 5; // add $sum = $num1 + $num2; // subtract $diff = $num1 - $num2; // multiply $product = $num1 * $num2; // divide $quotient = $num1 / $num2; // modulus $remainder = $num1 % $num2; ?> |
To perform an arithmetic operation simultaneously with an assignment, use the
two operators together. The following two code snippets are equivalent:
<?php
$a = $a + 10;
?>
<?php
$a += 10;
?>
Operator | What It Does |
= | Assignment |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division; returns quotient |
% | Division; returns modulus |
&& | Logical AND |
|| | Logical OR |
xor | Logical XOR |
! | Logical NOT |
++ | Addition by 1 |
0 comments:
Post a Comment