Summary of “Numbers and Strings in PHP”

The intval Function

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:

muffin_log(intval('38 parrots')); // Outputs: "38"
muffin_log(intval('Friday the 13th'));  // Outputs: "0"

The ternary operator

The ternary operator is useful when you need to choose one of two values:

condition ? value-1 : value-2.

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 class="<?= get_product_is_new($id) ? 'new' : 'old' ?>">...</p>

The ternary operator has shorthand notation:

value-1 ?: value-2

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.

$page = intval($_GET['page']) ?: 1;

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.

The for Loop

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.

for (before the loop; condition; after the iteration) {
  loop body
}

The code in parentheses has three parts, and they are separated by semicolons:

  • The code that will be executed once before the loop starts. Usually, a counter variable is declared here.
  • The condition that will be evaluated before each iteration 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.
  • The code that will be executed after each iteration. This is usually where the counter variable is increased.

Just like in other loops, the body of the for loop is written inside curly braces.

for ($i = 1; $i <= 9; $i = $i + 1) {
  muffin_log($i);
}

In order to insert the for loop in the template, we will use the following syntax:

  • The PHP tags are written in full: <?php ?>;
  • Instead of curly braces, we will write a colon: for ():;
  • The end of the loop is indicated by the endfor command, which is followed by a semicolon.
<ol>
<?php for ($i = 1; $i <= 9; $i = $i + 1): ?> // Start of the loop
  <li>...</li>                               // Body of the loop
<?php endfor; ?>                            // End of the loop
</ol>

The ceil Function

The ceil function accepts a number, and if it is a fraction, then it rounds it up to the nearest integer:

muffin_log(ceil(3.5));     // Outputs: "4"
muffin_log(ceil(3.99999)); // Outputs: "4"
muffin_log(ceil(3.00001)); // Outputs: "4"

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.

muffin_log(floor(3.9)); // Outputs: "3"
muffin_log(round(3.4)); // Outputs: "3"
muffin_log(round(3.6)); // Outputs: "4"

The date Function

The date function accepts a template string, and it returns the date in the specified format.

$now = date('H:i:s d.m.Y');

The date format is specified in the template string using control characters. Here are a few of them:

CharacterValue
dDay of the month, two digits with leading zeros
jDay of the month with a leading zero
FFull name of the month (in English)
mSequential number of the month with a leading zero
pSequential number of the month without a leading zero
YSequential number of the year, four digits
yNumber of the year, two digits
HHours in the 24-hour format with a leading zero
iMinutes with a leading zero
sSeconds with a leading zero

A complete list of characters can be found in the documentation.

The function returns the characters that are not control characters unchanged. Therefore, we can use spaces, dots, and the like in the template string.

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 documentation.

Single and Double Quotes

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:

$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"

Double quotes can help you avoid a confusing concatenation. You can learn more about the differences between the quotation marks in the documentation.


Continue