0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<h2>Conditional statements</h2>
1
<h2>Conditional statements</h2>
2
<p>A special statement that allows you to perform certain actions depending on the satisfaction of certain conditions.</p>
2
<p>A special statement that allows you to perform certain actions depending on the satisfaction of certain conditions.</p>
3
<p>Statement syntax:</p>
3
<p>Statement syntax:</p>
4
if ($temperature > 20) { muffin_log('Good weather!') // These actions will be executed if the condition in parentheses is true. }<p>The first part of the statement is the word if, which is called the<b>conditional operator</b>. Next comes the<b>condition</b>in parentheses, and in the curly braces you will find the actions, which are also called the<b>condition body</b>.</p>
4
if ($temperature > 20) { muffin_log('Good weather!') // These actions will be executed if the condition in parentheses is true. }<p>The first part of the statement is the word if, which is called the<b>conditional operator</b>. Next comes the<b>condition</b>in parentheses, and in the curly braces you will find the actions, which are also called the<b>condition body</b>.</p>
5
<h2>Alternative conditions</h2>
5
<h2>Alternative conditions</h2>
6
<p>else is used to indicate an alternative action option in case the condition inside if is not executed.</p>
6
<p>else is used to indicate an alternative action option in case the condition inside if is not executed.</p>
7
<p>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.</p>
7
<p>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.</p>
8
if (condition) { actions; } else { other actions; }<h2>Embedding a condition in a template</h2>
8
if (condition) { actions; } else { other actions; }<h2>Embedding a condition in a template</h2>
9
<p>PHP allows you to use conditions inside templates to change the page markup depending on the situation.</p>
9
<p>PHP allows you to use conditions inside templates to change the page markup depending on the situation.</p>
10
<?php if (condition): ?> page markup <?php endif; ?><h2>Logical (Boolean) values</h2>
10
<?php if (condition): ?> page markup <?php endif; ?><h2>Logical (Boolean) values</h2>
11
<p>There are only two Boolean values: true and false. Boolean statements are called logical operations.</p>
11
<p>There are only two Boolean values: true and false. Boolean statements are called logical operations.</p>
12
$is_new = true; if ($is_new) { muffin_log('New release!'); } // The following is displayed in the console: "New release!"<p>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.</p>
12
$is_new = true; if ($is_new) { muffin_log('New release!'); } // The following is displayed in the console: "New release!"<p>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.</p>
13
<h2>The OR operator</h2>
13
<h2>The OR operator</h2>
14
<p>The logical operator OR is used when there are several conditions and we can only execute the action if <i>any one of them</i>is true. It is indicated using the symbol ||.</p>
14
<p>The logical operator OR is used when there are several conditions and we can only execute the action if <i>any one of them</i>is true. It is indicated using the symbol ||.</p>
15
<p>For example:</p>
15
<p>For example:</p>
16
$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. }<p>Commands in the condition body will be executed only when one or both conditions in parentheses are true.</p>
16
$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. }<p>Commands in the condition body will be executed only when one or both conditions in parentheses are true.</p>
17
<h2>The AND operator</h2>
17
<h2>The AND operator</h2>
18
<p>The logical operator AND is used when there are several conditions and we can only execute the action if <i>all of one of them</i>are true. It is indicated using the symbol &&.</p>
18
<p>The logical operator AND is used when there are several conditions and we can only execute the action if <i>all of one of them</i>are true. It is indicated using the symbol &&.</p>
19
<p>For example:</p>
19
<p>For example:</p>
20
$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<b>and</b>the temperature is greater than 22 degrees Celsius, // then we will skip out on work. }<h2>Concatenation</h2>
20
$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<b>and</b>the temperature is greater than 22 degrees Celsius, // then we will skip out on work. }<h2>Concatenation</h2>
21
<p>The operation of concatenating several strings of text into one.</p>
21
<p>The operation of concatenating several strings of text into one.</p>
22
$product_class = 'item' . ' item-hot'; // We obtain the string 'item item-hot', and all spaces are preserved.<h2>Adding a class to the page markup</h2>
22
$product_class = 'item' . ' item-hot'; // We obtain the string 'item item-hot', and all spaces are preserved.<h2>Adding a class to the page markup</h2>
23
<h3>Conditions in the template</h3>
23
<h3>Conditions in the template</h3>
24
<section class="item <?php if ($is_new): ?> item-new <?php endif; ?> "><p>The item-new class in the condition body is not wrapped in quotation marks. We need to insert a <b>space</b>before item-new to ensure that the two classes are not concatenated together into a single word.</p>
24
<section class="item <?php if ($is_new): ?> item-new <?php endif; ?> "><p>The item-new class in the condition body is not wrapped in quotation marks. We need to insert a <b>space</b>before item-new to ensure that the two classes are not concatenated together into a single word.</p>
25
<h3>Script condition</h3>
25
<h3>Script condition</h3>
26
<p>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.</p>
26
<p>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.</p>
27
<p>In the page markup:</p>
27
<p>In the page markup:</p>
28
<section class="<?= $product_class ?>"><p>In the script:</p>
28
<section class="<?= $product_class ?>"><p>In the script:</p>
29
$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. };<h2>Arithmetic operators</h2>
29
$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. };<h2>Arithmetic operators</h2>
30
<p>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<a>specification</a>.</p>
30
<p>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<a>specification</a>.</p>
31
<ul><li>When conditions are added, full notation PHP tags must be used as opposed to shorthand notation;</li>
31
<ul><li>When conditions are added, full notation PHP tags must be used as opposed to shorthand notation;</li>
32
<li>You must write a colon after if instead of curly braces;</li>
32
<li>You must write a colon after if instead of curly braces;</li>
33
<li>You indicate the end of a condition in the template using the endif command followed by a semicolon.</li>
33
<li>You indicate the end of a condition in the template using the endif command followed by a semicolon.</li>
34
</ul><a>Continue</a>
34
</ul><a>Continue</a>