HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-03-09
1 <h2>The intval Function</h2>
1 <h2>The intval Function</h2>
2 <p>The intval function takes any value and always returns a number. If you pass a regular number to it, it will return it. But if you pass it a string, the function will try to extract a number from it. It can obtain a number from a string if it comes at the very beginning. If there is no number in the string, or if it does not come first, then the function will return 0:</p>
2 <p>The intval function takes any value and always returns a number. If you pass a regular number to it, it will return it. But if you pass it a string, the function will try to extract a number from it. It can obtain a number from a string if it comes at the very beginning. If there is no number in the string, or if it does not come first, then the function will return 0:</p>
3 muffin_log(intval('38 parrots')); // Outputs: "38" muffin_log(intval('Friday the 13th')); // Outputs: "0"<h2>The ternary operator</h2>
3 muffin_log(intval('38 parrots')); // Outputs: "38" muffin_log(intval('Friday the 13th')); // Outputs: "0"<h2>The ternary operator</h2>
4 <p>The ternary operator is useful when you need to choose one of two values:</p>
4 <p>The ternary operator is useful when you need to choose one of two values:</p>
5 condition ? value-1 : value-2.<p>If the condition is true, then PHP uses value -1, but if it is false, then it uses value-2. A question mark is inserted after the condition, and the values are separated by a colon.</p>
5 condition ? value-1 : value-2.<p>If the condition is true, then PHP uses value -1, but if it is false, then it uses value-2. A question mark is inserted after the condition, and the values are separated by a colon.</p>
6 &lt;p class="&lt;?= get_product_is_new($id) ? 'new' : 'old' ?&gt;"&gt;...&lt;/p&gt;<p>The ternary operator has shorthand notation:</p>
6 &lt;p class="&lt;?= get_product_is_new($id) ? 'new' : 'old' ?&gt;"&gt;...&lt;/p&gt;<p>The ternary operator has shorthand notation:</p>
7 value-1 ?: value-2<p>The shorthand notation works as follows: if value-1 is true, then it is used. But if it is false, then value-2 is used.</p>
7 value-1 ?: value-2<p>The shorthand notation works as follows: if value-1 is true, then it is used. But if it is false, then value-2 is used.</p>
8 $page = intval($_GET['page']) ?: 1;<p>The number 0, the strings '' and '0', the boolean value false, and arrays with no elements are all considered to be false in PHP. The other numbers, strings, and arrays are considered to be true.</p>
8 $page = intval($_GET['page']) ?: 1;<p>The number 0, the strings '' and '0', the boolean value false, and arrays with no elements are all considered to be false in PHP. The other numbers, strings, and arrays are considered to be true.</p>
9 <h2>The for Loop</h2>
9 <h2>The for Loop</h2>
10 <p>The for loop is similar to while, but its syntax is more compact, and when you use it, it is very hard to forget about the counter.</p>
10 <p>The for loop is similar to while, but its syntax is more compact, and when you use it, it is very hard to forget about the counter.</p>
11 for (before the loop; condition; after the iteration) { loop body }<p>The code in parentheses has three parts, and they are separated by semicolons:</p>
11 for (before the loop; condition; after the iteration) { loop body }<p>The code in parentheses has three parts, and they are separated by semicolons:</p>
12 <ul><li>The code that will be executed<b>once</b>before the loop starts. Usually, a counter variable is declared here.</li>
12 <ul><li>The code that will be executed<b>once</b>before the loop starts. Usually, a counter variable is declared here.</li>
13 <li>The condition that will be evaluated<b>before each iteration</b>of the loop. If the condition is true, the code inside the body will be executed, and if it is false, the loop will terminate.</li>
13 <li>The condition that will be evaluated<b>before each iteration</b>of the loop. If the condition is true, the code inside the body will be executed, and if it is false, the loop will terminate.</li>
14 <li>The code that will be executed<b>after each iteration</b>. This is usually where the counter variable is increased.</li>
14 <li>The code that will be executed<b>after each iteration</b>. This is usually where the counter variable is increased.</li>
15 </ul><p>Just like in other loops, the body of the for loop is written inside curly braces.</p>
15 </ul><p>Just like in other loops, the body of the for loop is written inside curly braces.</p>
16 for ($i = 1; $i &lt;= 9; $i = $i + 1) { muffin_log($i); }<p>In order to insert the for loop in the template, we will use the following syntax:</p>
16 for ($i = 1; $i &lt;= 9; $i = $i + 1) { muffin_log($i); }<p>In order to insert the for loop in the template, we will use the following syntax:</p>
17 <ul><li>The PHP tags are written in full: &lt;?php ?&gt;;</li>
17 <ul><li>The PHP tags are written in full: &lt;?php ?&gt;;</li>
18 <li>Instead of curly braces, we will write a colon: for ():;</li>
18 <li>Instead of curly braces, we will write a colon: for ():;</li>
19 <li>The end of the loop is indicated by the endfor command, which is followed by a semicolon.</li>
19 <li>The end of the loop is indicated by the endfor command, which is followed by a semicolon.</li>
20 </ul>&lt;ol&gt; &lt;?php for ($i = 1; $i &lt;= 9; $i = $i + 1): ?&gt; // Start of the loop &lt;li&gt;...&lt;/li&gt; // Body of the loop &lt;?php endfor; ?&gt; // End of the loop &lt;/ol&gt;<h2>The ceil Function</h2>
20 </ul>&lt;ol&gt; &lt;?php for ($i = 1; $i &lt;= 9; $i = $i + 1): ?&gt; // Start of the loop &lt;li&gt;...&lt;/li&gt; // Body of the loop &lt;?php endfor; ?&gt; // End of the loop &lt;/ol&gt;<h2>The ceil Function</h2>
21 <p>The ceil function accepts a number, and if it is a fraction, then it rounds it up to the nearest integer:</p>
21 <p>The ceil function accepts a number, and if it is a fraction, then it rounds it up to the nearest integer:</p>
22 muffin_log(ceil(3.5)); // Outputs: "4" muffin_log(ceil(3.99999)); // Outputs: "4" muffin_log(ceil(3.00001)); // Outputs: "4"<p>PHP also has two other rounding functions: floor and round. The first rounds down, and the second rounds either up or down to the nearest integer.</p>
22 muffin_log(ceil(3.5)); // Outputs: "4" muffin_log(ceil(3.99999)); // Outputs: "4" muffin_log(ceil(3.00001)); // Outputs: "4"<p>PHP also has two other rounding functions: floor and round. The first rounds down, and the second rounds either up or down to the nearest integer.</p>
23 muffin_log(floor(3.9)); // Outputs: "3" muffin_log(round(3.4)); // Outputs: "3" muffin_log(round(3.6)); // Outputs: "4"<h2>The date Function</h2>
23 muffin_log(floor(3.9)); // Outputs: "3" muffin_log(round(3.4)); // Outputs: "3" muffin_log(round(3.6)); // Outputs: "4"<h2>The date Function</h2>
24 <p>The date function accepts a template string, and it returns the date in the specified format.</p>
24 <p>The date function accepts a template string, and it returns the date in the specified format.</p>
25 $now = date('H:i:s d.m.Y');<p>The date format is specified in the template string using control characters. Here are a few of them:</p>
25 $now = date('H:i:s d.m.Y');<p>The date format is specified in the template string using control characters. Here are a few of them:</p>
26 CharacterValuedDay of the month, two digits with leading zerosjDay of the month with a leading zeroFFull name of the month (in English)mSequential number of the month with a leading zeropSequential number of the month without a leading zeroYSequential number of the year, four digitsyNumber of the year, two digitsHHours in the 24-hour format with a leading zeroiMinutes with a leading zerosSeconds with a leading zero<p>A complete list of characters can be found in the<a>documentation</a>.</p>
26 CharacterValuedDay of the month, two digits with leading zerosjDay of the month with a leading zeroFFull name of the month (in English)mSequential number of the month with a leading zeropSequential number of the month without a leading zeroYSequential number of the year, four digitsyNumber of the year, two digitsHHours in the 24-hour format with a leading zeroiMinutes with a leading zerosSeconds with a leading zero<p>A complete list of characters can be found in the<a>documentation</a>.</p>
27 <p>The function returns the characters that are not control characters unchanged. Therefore, we can use spaces, dots, and the like in the template string.</p>
27 <p>The function returns the characters that are not control characters unchanged. Therefore, we can use spaces, dots, and the like in the template string.</p>
28 <p>By default, the date function returns the time of the time zone specified in the PHP settings on the server. The time zone can be changed from the script using the date_default_timezone_set function. You can read more details in the<a>documentation</a>.</p>
28 <p>By default, the date function returns the time of the time zone specified in the PHP settings on the server. The time zone can be changed from the script using the date_default_timezone_set function. You can read more details in the<a>documentation</a>.</p>
29 <h2>Single and Double Quotes</h2>
29 <h2>Single and Double Quotes</h2>
30 <p>PHP uses both single ' as well as double " quotes for strings. They work the same in most cases, though not always. For example, if you use a variable inside single quotes, then its name will be output. And if you use it inside double quotes, then the value will be:</p>
30 <p>PHP uses both single ' as well as double " quotes for strings. They work the same in most cases, though not always. For example, if you use a variable inside single quotes, then its name will be output. And if you use it inside double quotes, then the value will be:</p>
31 $name = 'Dumpo'; muffin_log('The elephant is called $name'); // Outputs: "The elephant is called $name" muffin_log("The elephant is called $name"); // Outputs: "The elephant is called Dumpo"<p>Double quotes can help you avoid a confusing concatenation. You can learn more about the differences between the quotation marks in the<a>documentation</a>.</p>
31 $name = 'Dumpo'; muffin_log('The elephant is called $name'); // Outputs: "The elephant is called $name" muffin_log("The elephant is called $name"); // Outputs: "The elephant is called Dumpo"<p>Double quotes can help you avoid a confusing concatenation. You can learn more about the differences between the quotation marks in the<a>documentation</a>.</p>
32 <a>Continue</a>
32 <a>Continue</a>