PHP Expressions

There are several building blocks of coding in PHP 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.

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. 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.

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".

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.

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. It assigns the sum of the values of $Margaritaville + $Sun_Tan_Application to $Fun_in_the_Sun.

There really isn't much more to understand about expressions except for the assembly of them into compound expressions and statements using operators.