PHP Expressions and Control Flow: The previous Tutorial introduced several topics in passing that this Tutorial covers more fully, such as making choices (branching) and creating complex expressions. In the previous Tutorials, I wanted to focus on the most basic syntax and operations in PHP, but I couldn’t void touching on more advanced topics. Now I can fill in the background that you need to use these powerful PHP features properly. In this Tutorial, you will get a thorough grounding in how PHP programming works in practice and in how to control the flow of the program.
PHP Expressions
Let’s start with the most fundamental part of any programming language: expressions. An expression is a combination of values, variables, operators, and functions that result in a value. It’s familiar to anyone who has taken high-school algebra:
y = 3(abs(2x) + 4) which in PHP would be: $y = 3 * (abs(2 * $x) + 4);
The value returned (y, or $y in this case) can be a number, a string, or a Boolean value (named after George Boole, a nineteenth-century English mathematician and philosopher). By now, you should be familiar with the first two value types, but I’ll explain the third.
TRUE or FALSE?
A basic Boolean value can be either TRUE or FALSE. For example, the expression “20 > 9” (20 is greater than 9) is TRUE, and the expression “5 == 6” (5 is equal to 6) is FALSE. (You can combine Boolean operations using operators such as AND, OR, and XOR, which
are covered later in this Tutorial.)
Note that I am using uppercase letters for the names TRUE and FALSE. This is because they are predefined constants in PHP. You can also use the lowercase versions, if you prefer, as they are also predefined. In fact, the lowercase versions are more stable, because PHP does not allow you to redefine them; the uppercase ones may be redefined— something you should bear in mind if you import third-party code.
Example 4-1 shows some simple expressions: the two I just mentioned, plus a couple more. For each line, it prints out a letter between a and d, followed by a colon and the result of the expressions. The <br> tag is there to create a line break and thus separate the output into four lines in HTML.
Now that we are fully into the age of HTML5, and XHTML is no longer being planned to supersede HTML, you do not need to use the self-closing form of the tag, or any void elements (ones without closing tags), because the / is now optional. Therefore, I have chosen to use the simpler style in this book. If you ever made HTML non-void tags self-closing (such as), they will not work in HTML5 because the / will be ignored, and you will need to replace them with, for example,…
However, you must still use the form of HTML syntax when using XHTML.
Example 4-1. Four simple Boolean expressions
<?php echo "a: [" . (20 > 9) . "]<br>"; echo "b: [" . (5 == 6) . "]<br>"; echo "c: [" . (1 == 0) . "]<br>"; echo "d: [" . (1 == 1) . "]<br>"; ?>
The output from this code is as follows: a: [1] b: [] c: [] d: [1]
Notice that both expressions a: and d: evaluate to TRUE, which has a value of 1. But b: and c: which evaluate to FALSE, do not show any value because in PHP the constant
FALSE is defined as NULL or nothing. To verify this for yourself, you could enter the code in Example 4-2.
Example 4-2. Outputting the values of TRUE and FALSE
<?php // test2.php echo "a: [" . TRUE . "]<br>"; echo "b: [" . FALSE . "]<br>"; ?>
which outputs the following:
a: [1] b: []
By the way, in some languages, FALSE may be defined as 0 or even −1, so it’s worth checking on its definition in each language.
PHP Literals and Variables
The simplest form of an expression is a literal, which simply means something that evaluates to itself, such as the number 73 or the string “Hello”. An expression could also simply be a variable, which evaluates to the value that has been assigned to it. They are both types of expressions because they return a value.
Example 4-3 shows three literals and two variables, all of which return values, albeit of different types.
Example 4-3. Literals and variables
<?php $myname = "Brian"; $myage = 37; echo "a: " . 73 . "<br>"; // Numeric literal echo "b: " . "Hello" . "<br>"; // String literal echo "c: " . FALSE . "<br>"; // Constant literal echo "d: " . $myname . "<br>"; // String variable echo "e: " . $myage . "<br>"; // Numeric variable ?>
And, as you’d expect, you see a return value from all of these with the exception of c:, which evaluates to FALSE, returning nothing in the following output:
a: 73 b: Hello c: d: Brian e: 37
In conjunction with operators, it’s possible to create more complex expressions that evaluate to useful results. When you combine assignment or control-flow constructs with expressions, the result is a statement.
Example 4-4 shows one of each. The first assigns the result of the expression 366 – $day_number to the variable $days_to_new_year, and the second outputs a friendly message only if the expression $days_to_new_year < 30 evaluates to TRUE.
Example 4-4. An expression and a statement
<?php $days_to_new_year = 366 - $day_number; // Expression if ($days_to_new_year < 30) { echo "Not long now till new year"; // Statement } ?>
The summary of the PHP Expressions and Control Flow:
This tutorial focuses on PHP expressions and control flow. It starts by explaining the concept of expressions, which are combinations of values, variables, operators, and functions that result in a value. Examples of expressions are provided, including arithmetic calculations and function calls. The tutorial also introduces Boolean values, which can be either TRUE or FALSE, and demonstrates how to use comparison operators to evaluate expressions and obtain Boolean results.
The tutorial emphasizes that TRUE and FALSE are predefined constants in PHP, but lowercase versions can also be used. It highlights the importance of using the lowercase versions to avoid potential redefinition issues when importing third-party code. Several simple Boolean expressions are presented to illustrate the concept.
The next topic covered is literals and variables. A literal is a simple expression that evaluates to itself, such as a number or a string. Variables, on the other hand, store values and can be used in expressions. Examples of literals and variables are given, showcasing their different types and return values.
The tutorial concludes by introducing statements, which are formed by combining expressions with assignment or control-flow constructs. An example is provided where an expression calculates the number of days until New Year based on a variable, and a conditional statement outputs a message if the expression evaluates to TRUE.
Overall, this tutorial aims to provide a comprehensive understanding of PHP expressions, Boolean values, literals, variables, and their role in controlling the flow of a program.