Free PHP course. Sign Up for tracking progress →

PHP: Returning from loops

Dealing with loops usually comes down to two scenarios:

  1. Aggregation. Accumulating data over multiple iterations and handling it after the loop. String reversal is just one example of this.
  2. Executing a loop until you get the required result and then ending it. For example, if the job of the loop is to find prime numbers. Remember that a prime number is a number that can only be divided by itself and one.

Let's look at a simple algorithm to check prime numbers. We'll divide the number,x by all the numbers in the range from two to x - 1 and see the remainder. If we don't find a divisor x can be divided by without a remainder in this range, then we're looking at a prime number.

If you think about it, it's actually enough to check numbers not up tov x - 1, but up to half the given number. For example, 11 isn't divisible by 2, 3, 4, 5. But it's also guaranteed that it can't be divided by numbers greater than its half. So, we can do a little optimization and check the division only up to x / 2.

<?php

function isPrime($number)
{
    if ($number < 2) {
        return false;
    }

    $divisor = 2;

    while ($divisor <= $number / 2) {
        if ($number % $divisor === 0) {
            return false;
        }

        $divisor += 1;
    }

    return true;
}

isPrime(1); // false
isPrime(2); // true
isPrime(3); // true
isPrime(4); // false

The algorithm is built like so: if during the successive division by numbers up to x / 2 there is at least one result without a remainder, then the given argument is not a prime number, and therefore further calculations will be pointless. At this point, it returns false.

And only if the entire loop is completed can we say that the number is prime, since no number by which it can be divided without a remainder can be found.

Instructions

Implement a function called hasChar(), which checks if a string contains a letter in the specified case. The function takes two parameters:

  • A string
  • A letter to be found

Call example:

<?php

hasChar('Hexlet', 'H'); // true
hasChar('Hexlet', 'h'); // false
hasChar('Awesomeness', 'm'); // true
hasChar('Awesomeness', 'd'); // false
The exercise doesn't pass checking. What to do? 😶

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.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

Tips


If you got stuck and don't know what to do, you can ask a question in our community