Operators:
There is always use for math. If you're not a math person, no need to worry. We stick with easy math. So no calculus, trig, algreba, and so on.
We have 4 types of operators, and this goes for pretty much every other computer language out there.
-
Arithmetic Operators
-
Assignment Operators
-
Comparison Operators
-
Logical Operators
Arithemetic Operators:
Operator |
Name |
Examples |
Display |
+ |
Addition |
$x = 15;
echo $x + 5; |
20 |
- |
Subtraction |
$x = 15;
echo $x - 10; |
5 |
* |
Multiply |
$x = 15;
echo $x * 2; |
30 |
/ |
Divide |
$x = 15;
echo $x / 5; |
3 |
% |
Modulus |
$x = 15;
echo $x % 7; |
1 |
++ |
Increment |
$x = 15;
echo $x++; |
16 |
-- |
Decrement |
$x = 15;
echo $x--; |
14 |
Quick explanation of the last 3 rows:
the Modulus (%) is the remainder in a division. If you divide 15 by 7, you'll get 2 full parts, and 1 extra. That 1 extra is called the modulus.
the Increment (++) is very useful, especially in loops. It will add one to your variable.
the Decrement (--) is also useful. Exactly what you thought! Subtracts one from your variable.
Comparison Operators:
Operator |
Description |
Example |
Returns |
== |
Is equal to |
3==3
3==10 |
true
false |
!= |
Is not equal |
5!=10
5!=5 |
true
false |
<> |
Is not equal
(Same as !=) |
5<>10
5<>5 |
true
false |
> |
Greater than |
10>1
10>100 |
true
false |
>= |
Greater than or equal to |
10>=10
10>=12 |
true
false |
< |
Less than |
10<100
10<1 |
true
false |
<= |
Less than or equal to |
10<=10
10<=9 |
true
false |
Logical Operators:
Operator |
Description |
Example |
Returns |
&& |
and |
$x = 5;
$y = 10;
$x>0 && $y<20 |
true |
|| |
or |
$x = 5;
$y = 10;
$x ==5 || $y ==10 |
true |
! |
not |
$x = 5
$y = 10
!($x==$y) |
true |
These will make more sense when we get to if statements
Assignment Operators:
Operator |
Example |
Same as |
= |
$x = $y |
$x = $y |
+= |
$x += $y |
$x = $x + $y |
-= |
$x -= $y |
$x = $x - $y |
*= |
$x *= $y |
$x = $x * $y |
/= |
$x /= $y |
$x = $x / $y |
.= |
$x .= $y |
$x = $x . $y (like string concentation) |
%= |
$x %= $y |
$x = $x % $y |
Every operator can be put into a variable, changed, re-declared, and called (echo'd)
Remember:
When you get into large math formulars, the basic law of math: BEDMAS - Brackets, Exponents, Division/Multiplication, Addition/Subtraction
Your Task:
Create a new
php file called operators.php
Declare 2 integer variables. (Integers alone don't need quotes, remember)
Play with and figure out all the arithmetic operators and assignment operators.
Then make a sentence display with some text and one of your variables.
Don't worry about Logical or Comparison operators right now. By the end of this course, you'll be an operator proffessional!