example:
<?php
echo 'Hello, ' . 'world!';
echo 'Hello, world!' . '
';
?>
Two 'special' operators in PHP are not covered here and yet are used frequently. Before you look at them, though, it's important that you see how the comparison operators (such as <
, <=
, and !=
) are used inside conditional statements.
Conditional Statements
In a
if (
// action to take if condition is true
} else {
// optional action to take otherwise
}
The
part can be filled with any number of conditions you want PHP to evaluate, and this is where the comparison operators come into their own. For example:
if ($i > 10) {
echo '11 or higher';
} else {
echo '10 or lower';
}
PHP looks at the condition and compares $i
to 10. If it is greater than 10, it replaces the whole operation with 1
; otherwise, it replaces it with 0
. So, if $i
is 20, the result looks like this:
if (1) {
echo '11 or higher';
} else {
echo '10 or lower';
}
In conditional statements, any number other than 0 is considered to be equivalent to the Boolean value true
, so if (1)
always evaluates to true
. There is a similar case for strings: If your string has any characters in it, then it evaluates to true
, with empty strings evaluating to false
. This is important because you can then use that 1
in another condition through &&
or ||
operators. For example, if you want to check whether $i
is greater than 10 but less than 40, you could write this:
if ($i > 10 && $i < 40) {
echo '11 or higher';
} else {
echo '10 or lower';
}
If we presume that $i
is set to 50
, the first condition ($i > 10)
is replaced with 1
and the second condition ($i < 40)
is replaced with 0
. Those two numbers are then used by the &&
operator, which requires both the left and right operands to be true
. While 1
is equivalent to true
, 0
is not, so the &&
operand is replaced with 0 and the condition fails.
=
, ==
, ===
, and similar operators are easily confused and often the source of programming errors. The first, a single equal sign, assigns the value of the right operand to the left operand. However, all too often you see code like this:
if ($i = 10) {
echo 'The variable is equal to 10!';
} else {
echo 'The variable is not equal to 10';
}
That is incorrect. Rather than checking whether $i
is equal to 10, it assigns 10
to $i
and returns true
. What is needed is ==
, which compares two values for equality. In PHP, this is extended so that there is also ===
(three equal signs), which checks whether two values are identical — more than just equal.
The difference is slight but important: If you have a variable with the string value '10'
and compare it against the number value of 10, they are equal. Thus, PHP converts the type and checks the numbers. However, they are not identical. To be considered identical, the two variables must be equal (that is, have the same value) and be of the same data type (that is, both are strings, both are integers, and so on).
It is common practice to put function calls in conditional statements rather than direct comparisons. For example:
if (do_something()) {
If the do_something()
function returns true
(or something equivalent to true
, such as a nonzero number), the conditional statement evaluates to true
.
Special Operators
The ternary operator and the execution operator work differently from those you have seen so far. The ternary operator is rarely used in PHP, thankfully, because it is really just a condensed conditional statement. Presumably it arose through someone needing to make his code occupy as little space as possible because it certainly does not make PHP code any easier to read!
The ternary operator works like this:
$age_description = ($age < 18) ? 'child' : 'adult';
Without explanation, that code is essentially meaningless; however, it expands into the following five lines of code:
if ($age < 18) {
$age_description = 'child';
} else {
$age_description = 'adult';
}
The ternary operator is so named because it has three operands: a condition to check ($age < 18
in the previous code), a result if the condition is true ('child'
), and a result if the