Logo
/
Programming
/
PHP Course
/

else if statement

PHP: else if statement

The getTypeOfSentence() function distinguishes only between questions and normal sentences. Let's add support for exclamation sentences to it:

<?php

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

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

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

    return "Sentence is {$sentenceType}";
}

print_r(getTypeOfSentence('Who?') . "\n"); // => Sentence is normal
print_r(getTypeOfSentence('No') . "\n");   // => Sentence is normal
print_r(getTypeOfSentence('No!') . "\n");  // => Sentence is exclamation

We've added a check for exclamation sentences. Technically this function works, but it treats questions incorrectly. It also has problems from the standpoint of semantics. The exclamation mark is checked in any case, even if a question mark has already been found. The else branch is described for the second condition, but not for the first. That's why a question sentence becomes normal.

To fix the situation, let's use one more feature of the conditional construction:

<?php

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

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

    return "Sentence is {$sentenceType}";
}

print_r(getTypeOfSentence('Who?') . "\n"); // => Sentence is question
print_r(getTypeOfSentence('No') . "\n");   // => Sentence is normal
print_r(getTypeOfSentence('No!') . "\n");  // => Sentence is exclamation

Now all the conditions are lined up in a single construction. The elseif keyword means "if the previous condition is not satisfied, but the current one is".

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

The logic of the function works like this. If the last character is ?, then 'question' is returned. If the last character is !, then 'exclamation' is returned. In all other cases, 'normal' is returned.

Only one of the code blocks belonging to the entire if construction will be executed.

Instructions

Implement the getTrafficLightAction() function, which takes a traffic light color and returns what the driver should do.

Rules:

  • 'green''go'
  • 'yellow''slow down'
  • 'red''stop'
  • Any other color → 'unknown'

Call examples:

<?php

getTrafficLightAction('green');  // 'go'
getTrafficLightAction('red');    // 'stop'
getTrafficLightAction('purple'); // 'unknown'

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!