Difference between revisions of "4.3.7. Define the operators"
From Computer Science
Mr Russell (Talk | contribs) |
Mr Russell (Talk | contribs) |
||
| Line 20: | Line 20: | ||
=== evaluates both values and data type | === evaluates both values and data type | ||
| − | | if ($x == $y) { do something } | + | | |
| − | if ($x === $y) {do something} | + | if ($x == $y) { do something } |
| + | if ($x === $y) {do something} | ||
'''Caution''' | '''Caution''' | ||
| − | if ($x = $y) {do something} will always return '''true''', and assign the value and type of $y to $x | + | if ($x = $y) {do something} |
| + | will always return '''true''', and assign the value and type of $y to $x | ||
|- | |- | ||
| ≠ | | ≠ | ||
Revision as of 20:02, 7 November 2016
All programming languages have a number of essential operators. Operators are the commands that compare, affect (change) or calculate something. The following essential operators are required byt he IB:
Comparison Operators
Comparison operators evaluate whether two variables are equal, and if not, how they are different:
| Operator | Name | PHP Syntax | Explaination | Usage example |
|---|---|---|---|---|
| = | Equals | == or === | Compares two numbers or strings to see if they are the same. Returns true or false
PHP == converts the variables to the same data type and evaluates the values === evaluates both values and data type |
if ($x == $y) { do something }
if ($x === $y) {do something}
Caution if ($x = $y) {do something}
will always return true, and assign the value and type of $y to $x |
| ≠ | Not Equal to | != | ||
| < | Less than | < | ||
| > | Greater than | > | ||
| <= | Less than or equal to | <= | ||
| >= | Greater than or equal to | >= |
Calculation Operators
Clculation operators perform basic arithmatic on variables and return a result:
| Operator | Name | PHP Syntax | Explaination | Usage example |
|---|---|---|---|---|
| + | Addition | + or ++ | Compares two numbers or strings to see if they are the same.
PHP: == converts the variables to the same data type and evaluates the values, while === evaluates both values and data type |
if ($x == $y) { do something } |
| - | Subtraction | - or -- | ||
| * | Multiplication | |||
| / | Floating Point division | / | ||
| Div | Integer Division | div | ||
| Mod | Modulus | mod |