| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. |
OperatorsThese tutorials are about PHP and its use for server-side Web programming. ExpressionsThe 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
The numbers operand: something that an operator takes action upon
The plus sign ( 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, 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 ConceptsPHP has many operators and many types of operators. They can be categorized as
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
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
Some operators are unary operators. This means that they only take one operand. For instance, the negation operator (
There is one ternary operator in PHP, which is a shorthand form for the 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. 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 ( 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. // Order of Precedenceorder 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.
Associativityassociativity: 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
Some operators have right to left associativity. The most common of these is the assignment operator. For instance, in the expression
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||