Free PHP course. Sign Up for tracking progress →

PHP: Reading documentation

Reading documentation is one of the most important skills for any developer.

The main section that programmers come back to all the time is the list of functions in the language. First, there are many of these functions and it's impossible to remember them all. Second, in PHP especially, there's a problem with inconsistency of parameters and function return values. So you constantly have to remember their order.

It uses its own syntax to describe functions. It looks a bit like PHP itself, but it's different, in other words, it's not PHP code, only a description of a function signature. Consider the round() function, which rounds a number:

round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] ) : float

The purpose of this format is to show the types, numbers, and default values of the input parameters, as well as the type of the output parameter. The last one is always in the rightmost position, right after the colon. In this case, the return value type is float.

The parameters are listed in parentheses after the function name, and separated by commas. Each parameter name is preceded by its type. For example, the $val parameter is a float parameter. The default value is set by assignment, e.g. $precision defaults to zero.

Square brackets [] show optional function parameters, and they're also separated by commas. For the function above, these are $precision and $mode. $mode, in turn, defaults to the value of the PHP_ROUND_HALF_UP constant. If any parameter is equal to the value of a constant, then the documentation for that function lists and describes all possible constants that can be used as the value of that parameter

Based on the above, we can call the function as follows:

<?php

round(5.3); // 5.0
round(8.333, 1); // 8.3
round(8.333, 2, PHP_ROUND_HALF_UP); // 8.33

Instructions

Implement a function called getAge(), which rounds down someone's age. That is, if a person is ten and a half years old, the function should return 10.

Use the standard floor() function, which rounds down the number.

<?php

getAge(10);  // 10.0
getAge(9.1); // 9.0
getAge(8.9); // 8.0
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