Section 4.1. Expressions


4.1. Expressions

There are several building blocks of coding that you need to understand: statements, expressions, and operators. A statement is code that performs a task. Statements themselves are made up of expressions and operators. An expression is a piece of code that evaluates to a value. A value is a number, a string of text, or a Boolean.

A Boolean is an expression that results in a value of either trUE or FALSE. For example, the expression 10 > 5 (10 is greater than 5) is a Boolean expression because the result is trUE. All expressions that contain relational operators, such as the less-than sign (<), are Boolean. The Boolean operators are AND, OR, XOR, NOR, and NOT.


An operator is a code element that acts on an expression in some way. For instance, a minus sign can be used to tell the computer to decrement the value of the expression after it from the expression before it. The most important thing to understand about expressions is how to combine them into compound expressions and statements using operators. So we're going to look at operators used to turn expressions into more complex expressions and statements.

The simplest form of expression is a literal or a variable. A literal evaluates to itself. Some examples of literals are numbers, strings, or constants. A variable evaluates to the value assigned to it. For instance, any of the expressions in Table 4-1 are valid.

Table 4-1. Valid expressions

Example

Type

1

A numeric value literal

"Becker Furniture"

A string literal

TRUE

A constant literal

$user_name

A variable with username as a string

1+1

A numeric value expression that evaluates to a literal


Although a literal or variable may be a valid expression, they aren't expressions that do anything. You get expressions to do things such as math or assignment by linking them together with operators.

Assignment is a symbol that represents a specific action. For example, a plus sign (+) is an operator that represents addition.


An operator combines simple expressions into more complex expressions by creating relationships between simple expressions 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 "3 + 4".

Figure 4-1 shows how the parts of an expression come together.

Figure 4-1. Operands and operators working together as an expression to form a value


The numbers 3 and 4 are each valid expressions. The equation 3 + 4 is also a valid expression, whose value, in this case, happens to be 7. The plus sign (+) is an operator. The numbers to either side of it are its arguments, or operands. An argument or operand is something an operator takes action on; for example, an argument or operand could be a directive from your housemate to empty the dishwasher, and the operator empties the dishwasher. Different operators have different types and numbers of operands. Operators can also be overloaded, which means that they do different things in different contexts.

You've probably guessed from this information that two or more expressions connected by operators are called an expression. You're right, as operators create complex expressions. The more subexpressions and operators, the longer and more complex the expression. But no matter what, as long as it equates to a value, it's still an expression.

When expressions and operators are assembled to produce a piece of code that actually does something, you have a statement. We discussed statements in Chapter 3. They end in semicolons, which is the programming equivalent of a complete sentence.

For instance, $Margaritaville + $Sun_Tan_Application is an expression. It equates to something, which is the sum of the values of $Margaritaville + $Sun_Tan_Application, but it doesn't do anything. While it's an expression, the output doesn't make any sense, but if you add the equals sign (=), $Fun_in_the_Sun = $Margaritaville + $Sun_Tan_Application;, you get a statement, because it does something. As Example 4-1 demonstrates, it assigns the sum of the values of $Margaritaville + $Sun_Tan_Application to $Fun_in_the_Sun.

Example 4-1. Sum of values

 <?php $Margaritaville = 3; // Three margaritas $Sun_Tan_Application = 2; // Two applications of sun tan $Fun_in_the_Sun = $Margaritaville + $Sun_Tan_Application; echo $Fun_in_the_Sun; ?> 

Example 4-1 outputs:

 5 

There really isn't much more to understand about expressions except for the assembly of them into compound expressions and statements using operators. Next, we're going to discuss operators that are used to turn expressions into more complex expressions and statements.



Learning PHP and MySQL
Learning PHP and MySQL
ISBN: 0596101104
EAN: 2147483647
Year: N/A
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net