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 first part of the statement is the word if, which is called the conditional operator. Next comes the condition in parentheses, and in the curly braces you will find the actions, which are also called the condition body.

Alternative conditions

else is used to indicate an alternative action option in case the condition inside if is not executed.

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; ?>

Logical (Boolean) values

There are only two Boolean values: true and false. Boolean statements 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 it 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 one 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 write the condition itself to 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.

  • When conditions are added, full notation PHP tags must be used as opposed to shorthand notation;
  • You must write a colon after if instead of curly braces;
  • You indicate the end of a condition in the template using the endif command followed by a semicolon.

Continue