Summary of “Conditions in PHP”
Conditional statements
A special statement that allows you to perform certain actions depending on the satisfaction of certain conditions.
Statement syntax:
if ($temperature > 20) {
muffin_log('Good weather!');
// These actions will be executed if the condition in parentheses is true.
}
The statement starts with the keyword if, followed by a condition in parentheses. The curly braces contain the body of the if statement: the code that runs when the condition is true.
Alternative conditions
else is used to indicate an alternative action option when the condition inside if is false.
To add this statement to the code, after the closing brace of if add the word else, curly braces, and inside the curly braces write the actions that need to be executed.
if (condition) {
actions;
} else {
other actions;
}Embedding a condition in a template
PHP allows you to use conditions inside templates to change the page markup depending on the situation.
<?php if (condition): ?>
page markup
<?php endif; ?>
- Use the full PHP tags
<?php ... ?>for conditions rather than the short echo tags<?= ... ?>; - Write a colon after the condition in parentheses instead of an opening curly brace;
- End the if statement in the template with
endiffollowed by a semicolon.
Logical (Boolean) values
There are only two Boolean values: true and false. Operations on Boolean values are called logical operations.
$is_new = true;
if ($is_new) {
muffin_log('New release!');
}
// The following is displayed in the console: "New release!"We are not comparing the value of the variable in parentheses with anything. If it is true, then the actions inside the condition body will be executed. If the value of the variable is false, then these actions will not be executed.
The OR operator
The logical operator OR is used when there are several conditions and we can only execute the action if any one of them is true. It is indicated using the symbol ||.
For example:
$mark = 3;
$clean = true;
if ($mark > 4 || $clean) {
muffin_log('Mom, please buy me an ice cream!');
// We will ask for an ice cream if we receive an "A" or clean up our room.
}Commands in the condition body will be executed only when one or both conditions in parentheses are true.
The AND operator
The logical operator AND is used when there are several conditions and we can only execute the action if all of them are true. It is indicated using the symbol &&.
For example:
$is_sunny = true;
$temperature = 25;
if ($is_sunny && $temperature > 22) {
muffin_log('I will call in sick and then go out for a stroll!');
// If it is sunny outside and the temperature is greater than 22 degrees Celsius,
// then we will skip out on work.
}Concatenation
The operation of concatenating several strings of text into one.
$product_class = 'item' . ' item-hot';
// We obtain the string 'item item-hot', and all spaces are preserved.
Adding a class to the page markup
Conditions in the template
<section class="item
<?php if ($is_new): ?>
item-new
<?php endif; ?>
">The item-new class in the condition body is not wrapped in quotation marks. We need to insert a space before item-new to ensure that the two classes are not concatenated together into a single word.
Script condition
Another option for embedding a class is to add a variable that will contain the final output to the page layout. This output is a string that will contain the necessary classes, and we write the condition itself in the script. That way the code in the template will not be so cumbersome, and it will be easier to read.
In the page markup:
<section class="<?= $product_class ?>">
In the script:
$product_class = 'item';
if ($discount > 1400 || $is_last) {
$product_class = $product_class . ' item-hot';
// The word 'item' is concatenated together with the word 'item-hot' in one string.
};Arithmetic operators
Programmers often use mathematical signs, which are called arithmetic operators. For example, the operator + adds values together, and the operator * multiplies whereas / divides. You can learn more about this in the language specification.
Continue