Logo

PHP: else

Look at the function below. It determines the type of a sentence by its last character: if the sentence ends with a question mark, the function returns Sentence is question, otherwise it returns Sentence is normal:

<?php

function getTypeOfSentence(string $sentence): string
{
    $lastChar = $sentence[-1];

    if ($lastChar === '?') {
        $sentenceType = 'question';
    } else {
        $sentenceType = 'normal';
    }

    return "Sentence is {$sentenceType}";
}

print_r(getTypeOfSentence('Hodor') . "\n");  // => Sentence is normal
print_r(getTypeOfSentence('Hodor?') . "\n"); // => Sentence is question

We added else and a new block in curly brackets. It is executed if the condition in if turns out to be false. You can also nest other if conditions inside the else block. Else means "otherwise", "in the other case".

┌───────────┐
      │ condition? │
      └─────┬─────┘
 true │           │ false
      ↓           ↓
┌──────────┐ ┌──────────┐
│ if body  │ │ else body│
└──────────┘ └──────────┘

An example of nested blocks:

<?php

$number = 10;

if ($number > 10) {
    print_r('Number is greater than 10');
} else {
    if ($number === 10) {
        print_r('Number is exactly 10');
    } else {
        print_r('Number is less than 10');
    }
}

There are two ways to design an if-else construct. Using the negation !==, you can change the order of the blocks:

<?php

function getTypeOfSentence(string $sentence): string
{
    $lastChar = $sentence[-1];

    if ($lastChar !== '?') {
        $sentenceType = 'normal';
    } else {
        $sentenceType = 'question';
    }

    return "Sentence is {$sentenceType}";
}

To make the construct easier to design, try to choose a check without negations and adapt the contents of the blocks to it.

The example of using else shows how important it is to keep track of which if each block belongs to:

<?php

// Wrong
function checkNumber(int $number): void
{
    if ($number > 0) {
        print_r("Number is positive\n");
    }

    if ($number > 10) {
        print_r("Number is greater than 10\n");
    } else {
        print_r("Number is not positive\n");
    }
}

checkNumber(3);
// => Number is positive
// => Number is not positive

In the example above, we forgot to nest the second if inside the first one, so the else now belongs to the second if. The program reports that the number is both positive and not positive at the same time.

<?php

// Right
function checkNumber(int $number): void
{
    if ($number > 0) {
        print_r("Number is positive\n");

        if ($number > 10) {
            print_r("Number is greater than 10\n");
        }
    } else {
        print_r("Number is not positive\n");
    }
}

checkNumber(3);
// => Number is positive

Now the second if is nested inside the first one, and the else is at the same level as the first if and is contrasted with it.

Instructions

Implement a function, normalizeUrl(), that normalizes data. It takes a site address and returns it with https:// at the beginning.

The function accepts addresses in the form ADDRESS or http://ADDRESS, but always returns the address in the form https://ADDRESS. The function can also receive an address that is already in the normalized form https://ADDRESS, in which case nothing needs to be changed.

Call examples:

<?php

normalizeUrl('https://ya.ru'); // 'https://ya.ru'
normalizeUrl('google.com');    // 'https://google.com'
normalizeUrl('http://ai.fi');  // 'https://ai.fi'

There are several ways to solve the task. One of them is to use the str_starts_with() function to check whether the argument string begins with the string http://, and then, based on that, add or not add https:// to it.

You will also most likely need to drop the unnecessary part at the beginning of the string. Remember, we covered a way to get a piece of a string using the substr() function? If not, here is a reminder:

<?php

// Take 2 characters from the beginning
print_r(substr('python', 0, 2)); // => py

So, using substr() you can also drop a certain number of characters:

<?php

// Drop the first 2 characters
print_r(substr('python', 2)); // => thon

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!