PHP Expressions
PHP can also be called an expression oriented language that is because everything you write in PHP is an expression. In an earlier example we had assigned a value of 100 to $a, an integer value of 100 or either a string 100 is being stored in the variable $a. You may also assign a string "hello" to $a by writing $a = hello; Now, if you were to write $b = $a, then $b is containing the string "hello".
Using PHP it is also possible to assign a function to a variable by writing $a = function_a(); and this is all possible because PHP is an expression-oriented language as well. Another good example of expression orientation is pre- and post-increment and decrement. This is achieved using variable++ and variable--. These are known as increment and decrement operators. For example: if you want to increment $a by 1, you can simply write '$a++' or '++$a'. But if you want to add more than 1 to $a, say 4, then you will have to write '$a++' or '++$a' multiple times. A better practice is to write '$a = $a + 4'. '$a + 4' evaluates to the value of $a plus 4, and is assigned back into $a, which results in incrementing $a by 4. This can also be written as '$a += 3' which executes faster than previous ways of incrementing values. Extending this, any two-place operator can be used in this operator-assignment mode, for example '$a -= 10' (subtract 10 from the value of $a), '$b *= 5' (multiply the value of $b by 5), etc.
Comparison expressions
A very common type of expressions are comparison expressions. These expressions evaluate to either FALSE or TRUE. PHP supports > (bigger than), >= (bigger than or equal to), == (equal), != (not equal), < (smaller than) and <= (smaller than or equal to). The language also supports a set of strict equivalence operators: === (equal to and same type) and !== (not equal to or not same type). These expressions are most commonly used inside conditional execution, such as if statements.
PHP Operators
An operator is something that you feed with one or more values which yields another value. There are three types of operators.
- Unary operator - which operates on only one value
- Binary operators
- Ternary operator - used to select between two expressions depending on a third one
Arithmetic Operators
It's like basic arithmetic. You add, multiply, subtract, divide.
<?php //Example Code for Arithmetic Operators -$a; // Negation $a + $b; //Addition - This adds values of $a and $b $a - $b; // Subtraction - Difference of $a and $b $a * $b; // Multiplication - Product of $a and $b $a / $b; // Division - Quotient of $a and $b - returns a float value anytime $a % $b; // Modulus - Remainder of $a divided by $b ?>
Assignment Operators
The basic assignment operator is "=" which means that the left operand gets set to the value of the expression on the right.
<?php //Example Code $a = 10; //sets value of $a to 10 $b = $a; //sets value of $b to the value of $a echo $b; //you will see output as 10 $a = ($b = 20) + 10; // $a is equal to 30 now, and $b has been set to 20 ?>
Comparison Operators
One of the most important operators are Comparison Operators. They help you compare two values. Take a look at this table:
| Example | Name | Result |
|---|---|---|
| $a == $b | Equal | TRUE if $a is equal to $b. |
| $a === $b | Identical | TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4) |
| $a != $b | Not equal | TRUE if $a is not equal to $b. |
| $a <> $b | Not equal | TRUE if $a is not equal to $b. |
| $a !== $b | Not identical | TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4) |
| $a < $b | Less than | TRUE if $a is strictly less than $b. |
| $a > $b | Greater than | TRUE if $a is strictly greater than $b. |
| $a <= $b | Less than or equal to | TRUE if $a is less than or equal to $b. |
| $a >= $b | Greater than or equal to | TRUE if $a is greater than or equal to $b. |
Remember! When comparing an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers.