6th December 2015
In
PHP
PHP Operators and conditional tests and events
By the end of this pots you should be able to:
- Identify and apply logical, arithmetic and relational operators to scripts
- Apply an assignment operator
What are operators?
Operators are symbols that transform the value of a variable or form comparisons against them.
Here are some examples of arithmetic operators:
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addition Operation Result: $c
";
$c = $a - $b;
echo "Substraction Operation Result: $c
";
$c = $a * $b;
echo "Multiplication Operation Result: $c
";
$c = $a / $b;
echo "Division Operation Result: $c
";
$c = $a % $b;
echo "Modulus Operation Result: $c
";
$c = $a++;
echo "Increment Operation Result: $c
";
$c = $a--;
echo "Decrement Operation Result: $c
";
Source: php.net/manual/en/language.operators.arithmetic.php
Logical Operators
//Test one: If both have something in them the condition will be TRUE - else FALSE
$a = 40;
$b = 1;
if( $a && $b ){
echo "TEST1 : Both a and b are true
";
}else{
echo "TEST1 : Either a or b is false
";
}
//Here we are using the 'and' operator
if( $a and $b ){
echo "TEST2 : Both a and b are true
";
}else{
echo "TEST2 : Either a or b is false
";
}
Source: php.net/manual/en/language.operators.logical.php
Note: the use of:
&&
and
and
. Both are valid though && is supposed carry more precedence. I suggest && is the preferred choice.
Comparison or Relational Operators
Comparison operators are similar to logic but this is about comparing values against one another rather than simply on/off.
$a = 10;
$b = 20;
/*Ternary operators are if/else statements in shorthand*/
/* If condition is true then assign a to result otheriwise b */
$result = ($a > $b ) ? $a : $b;
echo "TEST1 : Value of result is $result
";
/* If condition is true then assign a to result otheriwise b */
$result = ($a < $b ) ? $a : $b;
echo "TEST2 : Value of result is $result
";
/* most basic usage of ternary operators */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
/*TIP: when learning how to use ternary operators write them in if/else first*/
Assignment Operators
In brief…
- Assignment operators assign a value to a variable
- Similar to arithmetic variables such as ++ or –
- They hold a variable with a value in memory and add additional value/s to it
- Assignment operators allow you to do complex calculations
- VAT and Non-VAT items for example
$a = 42;
$b = 20;
$c = $a + $b; /* Assignment operator */
echo "Addition Operation Result: $c
";
$c += $a; /* c value was 42 + 20 = 62 */
echo "Add AND Assigment Operation Result: $c
";
$c -= $a; /* c value was 42 + 20 + 42 = 104 */
echo "Subtract AND Assignment Operation Result: $c
";
$c *= $a; /* c value was 104 - 42 = 62 */
echo "Multiply AND Assignment Operation Result: $c
";
$c /= $a; /* c value was 62 * 42 = 2604 */
echo "Division AND Assignment Operation Result: $c
";
$c %= $a; /* c value was 2604/42 = 62*/
echo "Modulus AND Assignment Operation Result: $c
";
Source: php.net/manual/en/language.operators.assignment.php
In this post we have explored…
- Identified and applied logical, arithmetic and relational operators to scripts
- Applied assignment operators and discussed where we might apply them
No Comments