Sherif's Tech Blog

Just another guy on the Internet with a keyboard…

PHP Syntax

Introduction To PHP Syntax

There are three basic concepts you first need to understand in order to write PHP code with ease and confidence. They are expressions, operators, and statements. They all fall under the broader category of syntax. These same concepts apply in general to a lot of different programming languages. So they have been well-explored and defined. They’re not very difficult by themselves, but they are the essential building blocks to writing and understanding PHP code like a professional.

Expressions

So what is an expression? In its simplest form an expression is anything that denotes value. Values come in many shapes and sizes so it can be tricky to point out an expression in your PHP code unless you first break it down to its simplest form and then expand on that understanding. The first example we’ll look at shows us just how simple an expression can be.

(1)

Believe it or not, this is an expression. It tells us that the constant 1 has the value of 1. The reason I put it in parenthesis is because most expressions in PHP are usually grouped in parenthesis. Grouping expressions can formulate a single complex expression that’s made up of multiple expressions. They don’t have to be, but this is a part of the language’s syntax as we’ll explore in further detail. Syntax in a programming language tells us what combinations of sequences of characters are considered acceptably structured. So, in PHP expressions can be grouped with parenthesis to make up a more complex expression. Now, its important to note that an expression can always be broken down into its simplest form. That means any expression, regardless of its value, can be evaluated to either a TRUE expression or a FALSE expression. This is called the truth value or truth in expression and we’ll discuss it in more detail ahead. To get a better understanding of expressions we’ll need to look at another underlying component in syntax that ties right into expressions.

Operators

There are many different PHP operators that do different things depending on how you use them syntactically. An operator is basically anything that interacts with one or more expressions directly. So for example PHP has some arithmetic operators that can do math. For example addition, subtraction, multiplication, and division. These operators are denoted by the symbols + (plus), - (minus or dash), * (asterisk), and / (forward slash), respectively. Since an expression is anything that denotes value and an operator is anything that interacts with these values then logically this interaction must also form an expression, or another value of its own. Let’s take a look at this interaction.

1 + 2

The addition operator, like all of the other basic arithmetic operators in PHP, takes the expression to its right and applies the operation of that expression to the expression on its left. What this means is we take 1 and apply it to the operation of addition with 2. Thus we get 1 plus 2, which forms the expression 3. And that’s it! It’s really that simple, but most people don’t think about just how much work expressions do on a low-level. To expand on this we’ll take a look a look at a more complex expression.

1 + 2 * 3 / 4 – 5

OK, so this seems like pretty basic math, but the underlying question you may not be asking yourself, right away, is how does PHP know in which order to do the math? There is more than one operator in the expression. Thus there can be more than one order in which to do the operations. Well, lets reconsider what we just explained in the previous example. The concept we explained about taking the expression to the right of the operator and applying to it the operation using the expression to the left of the operator is something called associativity. It’s how the interpreter chooses to associate expressions with their operators. All of the four arithmetic operators we discussed use left-associativity. However, if they are all using the same associativity why would they give different results if we did the following?

((1 + 2 * 3) / 4) – 5

We used the same exact operators on the same exact expressions, right? However, the first example results in PHP giving us a value of -2.5 whereas the second example gives us a result of -3.25. The difference actually comes from another basic underlying principle in PHP syntax called operator precedence. You can read more on PHP operator precedence here in the PHP documentation. The idea is generally simplistic, however. We group the inner-most expression into individual expressions based on both operator precedence and the associativity of those operators.

Operator precedence tells us which operators must take priority over other operators in an expression. In the case of arithmetic operators the multiplication and division operators take precedence over the addition and subtraction operators. So in the first example the first thing PHP will do is take from the entire expression (and since we did not group the expression into smaller expressions using parenthesis it is considered our inner-most expression) and group the operators with the highest precedence and operate on their expressions first. This is because we have grouped them into an inner-expression creating potentially more inner-most expressions. In our case that means multiplication and division.

  • We start by taking the expression to the left of the multiplication operator (since it came first in the expression – remember we’re moving from left-to-right) and that’s 2 and we apply the product to its right, which is 3 and we get 6.
  • Now, we take the expression to the left of the division operator (that’s now 6) and we apply the expression to its right, which is 4 in the operation of division and that gives us 6 divided by 4 equals 1.5
  • Next, we do the addition and subtraction, because they have the next level of precedence in the expression. So we take the expression to the left of the addition operator, which is 1, and we apply to it the addition of its right-most expression, which is now 1.5 and we get 2.5
  • Finally, we take the expression to the left of the subtraction operator, which is now 2.5 and we apply the expression to its right, which is 5, to conduct the operation of subtracting 5 from 2.5 and we get -2.5!

What happened in the second example is that we grouped certain expressions together and so PHP is forced to work with each group of expressions individually. Remember, the operator precedence applies to the inner-most expressions first. This means operator precedence only takes priority within each group of expressions. So in the second example we start with multiplication, then do the addition. That’s our inner-most expression. Second, we do the division – that’s our next inner-most expression. Finally, we can do the subtraction. At this point we have reduced everything down to a single expression – or value. Obviously if you do the math in this order you get a completely different result, which is -3.25 instead of -2.5 and that basically applies to order of operations in math as well.

Statements

Now that you have a fair understanding of expressions and operators lets consider for a moment what statements are and how they tie into all of this syntax. In PHP a statement must be terminated by a semicolon. That means by putting a semicolon at the end of an expression, we’ve syntactically structured a statement. It may not be very useful or it might not be doing anything obvious, but that’s really all you need to have a statement.

(1);

Taking our first example of an expression we turned it into a statement by adding a semicolon. You might think I’m joking, but I can assure you this code is syntactically correct and PHP will execute it if run through the interpreter. It’s not going to do anything useful, because all it does is return an operand and that’s not interesting without output, but it is a statement that’s made up of an expression and all PHP does is execute the statement by evaluating the expression. Earlier we mentioned the truth in value, which means PHP can evaluate any expression to what’s called a boolean true or false. In our case (1) can be evaluated to a boolean true. Expressions like the following (FALSE, 0, 0.0, ”, array(), NULL) would all evaluate to a boolean false and here’s how we can prove it.

var_dump((bool) FALSE); // bool(false)
var_dump((bool) 0); // bool(false)
var_dump((bool) 0.0); // bool(false)
var_dump((bool) ''); // bool(false)
var_dump((bool) array()) // bool(false)
var_dump((bool) NULL); // bool(false)

The var_dump() is a PHP function that sends output directly to the output stream with a human-readable representation of how PHP would evaluate a given expression. You may send multiple expressions to var_dump() by separating each expression with a comma. var_dump() is a useful tool to see what’s inside a data structure according to PHP’s primitive data types. It tells you what type any expression represents to PHP and a human-readable representation of its value. We’ll discuss functions and data types in more depth later on.

There are a lot more examples I could give you, but the idea is the simplest form of an expression can express one of two things. Either the absence of value or the presence of value. This is because in many cases you aren’t actually interested in the value of the expression itself, but whether or not the expression has any value at all. This black-and-white relationship is commonly evident in conditional and loop control structures, for example. These are cases where it makes sense that all you really want is a yes or no answer to the question does this expression represent the presence of value or the absence of value.

Your email address will not be published. Required fields are marked *

*