Arithmetic Operators
As you've seen in a previous example in The Basics, the assignment operator works just like its mathematical counterpart. This also applies to other operators such as +, -, * and /. If you've ever used the basic calculator on your computer, then these should be familiar to you as plus, minus, multiply and divide respectively.
Below is an example of how this feature can be used for simple arithmetic operations:
|
<?php
$value1 = 50;
// addition
// subtraction
// multiplication
// division ?> |
Be wary of spacing. If operations are written without spacing, they will not perform properly. This is then displayed with the echo code:
|
<?php echo $sum ?><br> <?php echo $difference ?><br> <?php echo $product ?><br> <?php echo $quotient ?><br> |
And we get something along the lines of:
150 (sum)
50 (difference)
5000 (product)
2 (quotient)
Note that if you have more than one operator acting in an expression, such as "$a - $b * $c", the normal mathematical precedence rules apply, so in this case the multiplication occurs before the subtraction.
String Operators
Operators aren't limited to just numbers. You can also add string variables with the string concatenation operator, which is represented by a full stop.
|
<?php
// choose the string variables
// combine the string variables
$sentence2 = $a . $d . $c . $b . '?'; ?> |
And this will print out two sentences; one being "tell me about him!" and the other being "tell him about me?". Notice how the punctuation is enclosed in quotation marks. You must do this to all non-variables for the script to be read properly.
Comparison Operators
Comparison Operators allow you to test things like whether one number value is larger, smaller or equal to another. Unlike expressions with Arithmetic Operators (which return a value), those with Comparison Operators evaluate to either true or false and are frequently used in conditional statements.
| Operand | Name | Example |
| == | Equal to | $variable1 == $variable2 |
| != | Not equal to | $variable1 != $variable2 |
| < | Less than | $variable1 < $variable2 |
| > | Equal to | $variable1 > $variable2 |
| <= | Less than or equal to | $variable1 <= $variable2 |
| >= | Greater than or equal to | $variable1 >= $variable2 |
So where exactly are comparison operators used? Well they're commonly found in user login systems. For example:
|
<?php
$storedpassword = 'mypassword';
if ($storedpassword == $enteredpassword) {
?>
|
This code could also be written in the inverse form where if $storedpassword != $enteredpassword then you get the message error message, anything else and you get the success message.
Logical Operators
Logical operators are also called Boolean operators because they are used for true or false expressions only.
| Operand | Name | Description |
| and | And | True if both lefthand and righthand expressions are true. |
| or | Or | True if either lefthand or righthand expression is true. |
| xor | Exclusive or | True if either lefthand or righthand expression is true but not when both are true. |
| ! | Not | True if the expression is not true. |
| && | And (different variation) | True if both lefthand and righthand expressions are true. Takes precendence (read first) over 'and'. |
| || | Or (different variation) | True if either lefthand or righthand expression is true. Takes precendence (read first) over 'or'. |
To add to the user login example for comparison operators, here's how the "and" operand works with the login system:
|
<?php
$username = 'myusername';
if ($username = 'myusername' and $password = 'mypassword') {
?>
|
The and can be replaced with &&. It's the same thing except '&&' is of higher precendence than 'and'. For all the logical operators, you will need two variables that are compared to one another.