[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
2. http basics
3. php basics
4. php expressions
* a. php operators
b. php math operators
c. php other operators
5. php client side
6. php flow control
7. php manual


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

Operators

These tutorials are about PHP and its use for server-side Web programming.

Expressions

The basic building block of a program is the statement. A statement is a piece of code that does something.

expression: a piece of code that can be evaluated to produce a value

Statements themselves are made up of expressions and operators. An expression is a piece of code that evaluates to some value.

The simplest form of expression is a literal or a variable. A literal evaluates to itself. A variable evaluates to the value assigned to it.

For instance, any of the following are valid expressions:

"abc"
123
$a
$x == 7
($a + $b) / $c

Although a literal or variable may be a valid expression, they are not expressions that do anything. The way you get expressions to do things is by linking simple expressions together with operators.

operator: combines simple expressions together into more complex expressions by creating relationships between the simple expresions that can be evaluated

An operator combines simple expressions together into more complex expressions by creating relationships between the simple expresions that can be evaluated. For instance, if the relation you want to establish is the cumulative joining of two numeric values together, you could write
6 + 7

The numbers 6 and 7 are each valid expressions. The equation 6 + 7 is also a valid expression, whose value, in this case, happens to be 13.

operand: something that an operator takes action upon

The plus sign (+) is an operator. The numbers to either side of it are said to be its arguments, or operands. An argument or operand is something that an operator takes action on. Different operators have different types and numbers of operands. (You will also find that some operands are overloaded. This means that they will do different things in different contexts.)

Two or more expressions connected by operators are called an expression. That's right. You use operators to make complex expressions. The more sub-expressions and operators, the longer and more complex the expression, but as long as it is something that equates to a value, it is still and expression.

When expressions and operators are assembled in such a way as to produce to a piece of code that actually does something, you have a statement. Statements end in semi-colons and are the programming equivalent of the complete sentence.

For instance, $a + $b is an expression. It equates to something, the sum of the values of $a and $b, but it doesn't do anything. $c = $a + $b; is, on the other hand, a statement, because it does something. It assigns the sum of the values of $a and $b to $c.

Since there isn't really much to understand about expressions except for the assembly of them into compound expressions and statements using operators, we are going to look at the operators used to turn expressions into more complex expressions and statements.

Operator Concepts

PHP has many operators and many types of operators. They can be categorized as

  • arithmetic operators,
  • string operators,
  • assigment operators,
  • autoincrement operators,
  • casting operators,
  • relational operators,
  • logical operators,
  • bitwise operators, and
  • a few others that don't really fit into any category.

Except for bitwise operators, we are going to look at all of these in more detail. Bitwise operators allow you to manipulate integer values at the binary level. This is not something you will need for anything but the most advanced PHP programs. Bitwise operations also require a strong grounding in computer logic at the level of logic gates, registers and memory addressing. We are going to stick with things you can make use of almost immediately.

When working with operators, you must be careful about paying attention to four aspects of the operator:

  • number of operands,
  • type of operands,
  • order of precedence, and
  • operator associativity.

Number of Operands

Different operators take different numbers of operands. Most operators are binary operators, which means they are used to combine two expressions into a single, more complex expression. We are most familiar with binary operators since most mathematical operators are binary operators, for instance $a + $b or $c / $d.

Some operators are unary operators. This means that they only take one operand. For instance, the negation operator ( - ) turns a numeric value into its negative value ( -7 ). PHP also has autoincrement and decrement as in C and C++ operators which are unary operators.

There is one ternary operator in PHP, which is a shorthand form for the if statement. We will discuss it when we talk about conditional expressions.

Types of Operands

When working with operators, you should pay attention to the type of operand an operator is meant to work on. Certain types of operators expect their operands to be of certain data types. Unlike some programming languages, PHP will try to convert the operands associated with an operator to the type of operand the operator is expecting. For instance, mathemetical operators only work on numbers. Even though PHP will try to convert non-numeric values to numeric values for you, it still can't multiply two strings together if they are not numbers. "abc" * "def" is not a valid expression.

This process of converting from one type to another is known as casting. PHP will try to take care of casting for you. This process is known as implicit casting. Different operators have different rules for how they cast their operands. You can also explicitly cast operands. This is discussed a little later.

Some operators are overloaded. An overloaded operator does different things in different contexts. For instance, the period ( . ) is a concatenation operator for strings, but is also the decimal point for floating point numbers and the separator between object names and property names.

Some binary operators only accept certain values as their left hand operator. This is true of most of the assignment operators, which assign the value that is the result of the expression on the right to the expression on the left. This means that the expression on the left has to be something you can assign a value to.

The left-hand expression in assignment operations is known as an L-value. An L-value is, simply, an expression that occurs on the left hand side of an assignment operator. An L-value expression must be an expression that can legitimately have a value assigned to it. The only valid L-values in PHP are variables, properties of objects, and elements of arrays.

//  A legal PHP expression
  $a = $b + $c;

//  An illegal PHP expression
// You cannot use the assignment operator to
// assign a value to a complex expression
  $a + $b = $c;

//  A pointlessly complex, but legal expression
// The parentheses create a specific order of 
// precedence, which is our next topic 
  $a = $b + ($c = $d / ($e = $f * $g));

Order of Precedence

order of precedence: the order in which operators are acted upon

Operators have an order or precedence, or an order in which they are processed in an expression. For instance, multiplication and division take precedence over addition and subtraction and everything else being equal will be processed first.

Operators that have the same precedence are processed in the order in which they appear in the expression. for instance, addition and subtraction have the same order of precedence, and so are processed in the order in which they appear.

Most operators that have the same precedence as one or another either normally only occur once in an expression or can be processed in any order in relation to each other. With addition and subtraction, if you start with ten, it does not matter whether you add five and subtract three, or subtract three and add five, the result is still twelve.

You can use parentheses within an expression to clarify or override the default order of precedence.

Using parentheses to mark of the order the expression would be processed in anyway makes the code easier to read. It doesn't change the order of processing, but it does clarify it.

Overriding the order of precedence means you want the operators to be processed in an order different from the default order.

Operators with the same precedence can occur in any order without affecting the result.

 10 + 5 - 3 == 12;
 10 - 3 + 5 == 12;

 10 * 2 / 5 == 4;
 10 / 5 * 2 == 4;

Operators with different orders of precedence must occur in a specific order or the results will be different

// default order
10 + 5 * 2 == 20;

// same, with order specified
10 + (5 * 2) == 20;

// order changed
(10 + 5) * 2 == 30;

For basic math, the operators with the highest order of precedence are multiplication and division, and the lowest is the assignment operator. The reality of programming is a bit more complex.

Here is a partial list of all operators in PHP by order of precendence. The list is in descending order, from highest to lowest. Operators in the same grouping have the same level of precedence. (The Associativity column lists those that are right to left instead of left to right. See the next section for an explanation of this.)

The grouping numbers are entirely arbitrary and are just meant to make the table easier to read in print form.

Note that this table is not complete.

Operator Description Operands Assoc.
Group 1
new create new object constructor call R to L
Group 2
. property access (dot notation) objects  
[ ] array index array[integer, string]  
( ) function call function(arguments)  
Group 3
! logical NOT unary R to L
~ bitwise NOT unary R to L
++, -- increment and decrement operators lvalue R to L
+, - unary plus and negation number R to L
(int), (double), (string), (array), (object) cast operators unary R to L
@ inhibit errors unary R to L
Group 4
*, /, % mutiplication, division, modulo (remainder) numbers  
Group 5
+, - addition, subtraction numbers  
. concatenation strings  
Group 6
<<, >> bitwise shift left, butwise shift right numbers, strings  
Group 7
<, <=, >, >= comparision operators: less than, less than or equal to, greater than, greater than or equal to numbers, strings  
Group 8
==, != equality, inequality any  
===, !== identity, non-identity any  
Group 9
& bitwise AND boolean  
Group 10
^ bitwise XOR boolean  
Group 11
| bitwise OR boolean  
Group 12
&& logical AND boolean  
Group 13
|| logical OR boolean  
Group 14
? : conditional (ternary) boolean ? any : any R to L
Group 15
= assignment lvalue = any R to L
*=, /=, %=, +=, -= assignment with operation lvalue *= any R to L
Group 16
and logical AND boolean  
Group 17
xor logical XOR boolean  
Group 18
or logical OR boolean  
Group 19
, list separator binary  

Associativity

associativity: the direction in which the operator is processed

All operators have associativity. Associativity is the direction in which the operator is processed.

Most operators have left to right associativity, which is to say that they are processed from left to right. In the expression $a + $b - $c;, $a and $b are added together and then $c is subtracted from the result.

Some operators have right to left associativity. The most common of these is the assignment operator. For instance, in the expression $a = $b = $c;, the value of $b is set to the value of $c, then the value of $a is set to the value of $b. Looking at the code, you would expect all three values to be equal, and this is what the expression does. If left to right associativity were used for assignment, this would not be the case.

[top]