Review of Operators

Main | Review of Operators | Mathematical Functions | Practice 5 | Solution 5 | HTML Forms | PHP Forms | Practice 6 | Solution 6 | PHP and E-mail | Practice 7 | Solution 7

During the first quarter of the course we used operators as they were needed to convey overall concepts, such as loops, conditionals, and functions. We will now review the same operators that we used before and introduce some new ones as well.

Recall that variables are denoted in PHP by preceding them with a dollar sign ($). So if I wanted to create a variable called a, I would type the following:

$a;

Recall also that if I wanted to assign $a a value, I would use the assignment operator:

$a = 3;

In the above example $a now has the value of 3.

The assignment operator is flexible in that it can be combined with other operators. We briefly covered the basic arithmetic operators during our discussion on variables. Consider the following statements:

$a = 2;
$b = 3;
$a = $a + $b;  

In the above we see that $a now has the value of 5. The above can be expressed in a more compact form as the following:

$a = 2;
$b = 3;
$a += $b;  

From the above you should gather that the assignment operator can be combined with an arithmetic operator, so that $a = $a + $b will have the same value as $a += $b.

Comparison Operators

As we saw when we discussed loops, PHP can compare a relation between two numbers. Consider the following comparisons between $a and $b:

$a < $b
$a > $b
$a <= $b
$a >= $b
$a == $b

All of the above examples could be put in the context of an if statement since they will all return true or false. Note that the last example makes a comparison which asks whether or not $a is equal to $b.

It is important to distinguish the assignment operator from the comparison operator. Consider the following statements:

$a = b
$a == $b

In the above $a is assigned the value of $b in the first statement. The first statement is not concerned with whether or not $a is actually equal to $b as in a comparison, since it simply assigns $a to the same value of $b, and thus they will then be equal. The second statement from the above is asking a question, and can return true or false. In this second statement both $a and $b keep their values and true is returned just in case $a and $b have the same value.

The Error Control Operator

You might recall our discussion on functions where we discussed giving a function's argument a default value to avoid possible warning messages. Remember that warning messages do not indicate a syntax bug with your program but instead inform you that something might be wrong with the way your program is running.

PHP gives you the ability to suppress error messages by allowing you to precede any warning message that a function may give by prepending the at sign (@) to the call of that function. So, so to suppress any warning that a function f($x) may give, simply call it in the following way:

@f($x)

In general it is not good to use this operator. Warning messages are there to help make your code better, so suppressing them with the @ operator is a cover-up that will probably lead to further bugs down the road. Use this operator with caution.

Logical Operators

We all ready covered these in our discussion of conditionals. As a review consider the following table:

ExampleNameResult
$a and $bAndTRUE if both $a and $b are TRUE.
$a or $bOrTRUE if either $a or $b is TRUE.
$a xor $bXorTRUE if either $a or $b is TRUE, but not both.
! $aNotTRUE if $a is not TRUE.
$a & & $bAndTRUE if both $a and $b are TRUE.
$a || $bOrTRUE if either $a or $b is TRUE.

Recall that PHP has two ways of expressing the relations "and" and "or", respectively: and and && as well as or and ||. In almost all cases you can use either operator interchangeably. PHP does however give higher precedence to && and || than it does to and and or. So if you had an expressions such as:

($a and $b && $c)

The the "and" of $b && $c would be computed first, and then it would be "anded" against $a. This distinction is so minor, that it is not worth worrying about.

If you are ever concerned about one operator's precedence over another and are unsure of how the language that you are programming in evaluates an expression, you can always rely on parenthesis to remove an ambiguity. For example, this statement

1 + 5 * 3

will evaluate to 16 as opposed to 18 since the multiplication ("*") operator has a higher precedence than the addition ("+") operator. However, there is no harm in expressing the same statement as:

1 + (5 * 3)

Naturally, if you wanted the addition to come first in the above expression (and change its meaning) you would express it as the following:

(1 + 5) * 3

It is also worth noting that PHP has an exclusive or operator (xor). Exclusive or is the type of "or" that you hear in a restaurant when you are offered soup or salad, it is distinct from the inclusive or operator in that you usually can't have both the soup and the salad.

PHP's "or" operator specifies an inclusive or operator (which we have covered). Xor is different in that it has the following truth table:

row statement A statement B A xor B
1 T T F
2 T F T
3 F T T
4 F F F

String Operators

The main string operator is the concatenation operator which looks like a period ("."). If we concatenate two strings then we combine them together. Consider the following example:

$a = "Hello ";
$a = $a . "World!"; // now $a contains "Hello World!"

The flexibilities of the assignment operator can be combined with the concatenation operator to result in more terse code. For example, the above code could be more cleanly expressed as the following:

$a = "Hello ";
$a .= "World!";  // now $a contains "Hello World!"

When using PHP with Databases the concatenating assignment operator (".=") is very useful for building long queries. We will talk more about that later.


jfulton [at] member.fsf.org
22 Aug 2013