Free PHP course. Sign Up for tracking progress →

PHP: Edge cases

The mysubstr(), function you implemented in the last lesson contains many errors. "But it passed the tests!" Yes, but there were no edge cases in these checks. The function worked fine with normal arguments, but how would it behave if you gave it these length options?

  • 0
  • A negative number
  • A number larger than the actual string size

The mysubstr() function isn't designed for this. You might think that this isn't a problem: the function works under normal conditions, and you simply need to not pass "bad" arguments to it. In a perfect world, yes, but in the real world your code might run in different situations, with different combinations of conditions and data. You can't be sure that arguments will always be correct, so you have to consider all cases, within the bounds of common sense.

Boundary errors are the most common cause of logical errors in programs. There's always something that programmers forget to take into account. These errors often don't manifest themselves immediately, and may not lead to visible problems for a long time. The program may continue to work, but at some point someone might notice an error in the results. Often, the reason is PHP's weak typing.

The ability to deal with such errors comes with experience, through constant "oops, forgot to check for an empty string!"-type screw-ups.


Let's imagine an extended version of the mysubstr() function. It takes three arguments; a string, an index, and the length of the substring to be extracted. The function returns a substring of the specified length starting from the specified index. Call examples:

<?php

$str = 'If I look back I am lost';
mysubstr($str, 0, 1); // 'I'
mysubstr($str, 3, 6); // 'I look'

Let's figure out what could go wrong. What edge cases are worth considering:

  • The substring extracted has a negative length
  • The index set is negative
  • The index set exceeds the boundary of the whole string
  • The length of the substring plus the given index exceeds the boundary of the whole string

When the function is implemented, each edge case will be a separate piece of code, most likely implemented with an if.

If you want to write a mysubstr() function that's protected against these cases, it's worth writing a separate function that will check the arguments for correctness. We'll get to that in the assignment.

Instructions

Implement an isArgumentsForSubstrCorrect predicate function that takes three arguments:

  1. the string
  2. the index from which to begin the extraction
  3. the length of the substring to be extracted

The function returns false if at least one of the conditions is true:

  • The substring extracted has a negative length
  • The index set is negative
  • The index set exceeds the boundary of the whole string
  • The length of the substring plus the given index exceeds the boundary of the whole string

Otherwise the function returns true.

Don't forget that indexes start with 0, so the index of the last element is "string length minus 1".

Call example:

<?php

$str = 'Sansa Stark';
isArgumentsForSubstrCorrect($str, -1, 3);  // false
isArgumentsForSubstrCorrect($str, 4, 100); // false
isArgumentsForSubstrCorrect($str, 10, 10); // false
isArgumentsForSubstrCorrect($str, 11, 1);  // false
isArgumentsForSubstrCorrect($str, 3, 3);   // true
isArgumentsForSubstrCorrect($str, 0, 5);   // true
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.