4.3.7. Define the operators

From Computer Science
Revision as of 20:42, 7 November 2016 by Mr Russell (Talk | contribs)

Jump to: navigation, search

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  != Compares two numbers or strings to see if they are not the same. Returns true if they are not the same and false if they are the same
if ($x != $y) { do something }
< Less than < Compares two numbers or strings to see if one is smaller than the other. Returns true if the first argument is smaller than the second, and false if they are equal or the second argument is smaller than the first

Number comparison

Integers can be compared with other integers or floating point numbers. the numeric greater is assessed. 9<10, and 99.8<99.9

String Comparison

Strings are assessed alphabetically according to the ASCII (or unicode) table. "Mary" < "Fred" , but be aware that "Anton" < "mike" (since lower case characters appear earlier in the ASCII table than capital letters"

Boolean Comparison

Since true is given the value of 1, and false = 0, false < true

Null Comparison

null values (which you may get from a database result set) are undefined, and therefore will behave erratically in comparisons. Always initialise variables.

if ($a < $b) { do something }
> Greater than > Compares two numbers or strings to see if one is larger than the other. Returns true if the first argument is larger than the second, and false if they are equal or the second argument is larger than the first

Number comparison

Integers can be compared with other integers or floating point numbers. the numeric greater is assessed. 10>9, and 99.9>99.8

String Comparison

Strings are assessed alphabetically according to the ASCII (or unicode) table. "Fred" > "Mary", but be aware that "mike" > "Anton" (since lower case characters appear earlier in the ASCII table than capital letters"

Boolean Comparison

Since true is given the value of 1, and false = 0, true > false

Null Comparison

null values (which you may get from a database result set) are undefined, and therefore will behave erratically in comparisons

if ($a < $b) { do something }
<= 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