PHP: Default Parameters
Some functions in PHP have optional parameters. This means that a default value is set for them in advance, and you can omit such a parameter when calling the function.
Let's look at the built-in round() function, which rounds a number:
<?php
$result = round(10.25, 0); // 10We passed two values to it:
- The number that needs to be rounded.
- The rounding precision;
0means that the number will be rounded to an integer.
Since most often you need exactly rounding to an integer, the creators of the round() function made the second parameter optional and gave it the default value 0. That's why the result will be the same even if you don't specify the second parameter:
<?php
$result = round(10.25); // 10If you need a different precision, you can specify it explicitly:
<?php
// rounding to one decimal place
$result = round(10.25, 1); // 10.3round(10.25, 1) → arguments: 10.25, 1 → 10.3
round(10.25) → arguments: 10.25, (0) → 10
└── default valueThe number of optional parameters depends on the specific function, but required ones always come before optional ones.
A function's signature
Every function has a signature that contains a description of its name, its parameters, and the order in which they are used. The signature helps you understand what data the function expects and what will happen if the parameters are not specified.
Let's look at the documentation for the round() function. If we simplify it a little, the signature looks like this:
round(int|float $num, int $precision = 0): floatThe function is called round. The $num parameter is required — it takes the number to round (an integer or a fraction). The $precision parameter has the default value 0, that is, it is optional; if you don't specify it, the number will be rounded to an integer. After the colon comes the type of the result — the function returns a fractional number (float).
The full signature from the documentation has one more optional parameter that controls the rounding mode. It is rarely needed, so we won't consider it for now.
How to work with new functions
When you come across a new function, you can use a simple pattern:
- Open the documentation and find the function's signature.
- Look at usage examples.
- Go to the interactive PHP shell (it is launched with the
php -acommand) and try calling the function with different arguments.
This approach helps you quickly figure out how exactly a function works, which of its parameters are required and which are optional, and what results it returns.
Instructions
The code contains data about a car trip: the distance, fuel consumption, fuel price, and the number of passengers.
Calculate and print three values to the screen:
- The fuel volume in liters needed for the trip. Round it to one decimal place.
- The total cost of the trip in rubles. Round it to two decimal places.
- The trip cost for each passenger in rubles. Round it to an integer by calling
round()without the second argument.
Each value is printed on a separate line.
distance ──────┐
├──→ fuel ──────┐
fuelConsumption ───┘ ├──→ tripCost ──────┐
│ ├──→ perPerson
fuelPrice ─────────────────────┘ │
│
passengers ────────────────────────────────────────┘Tips
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.
PHP: Default Parameters
Some functions in PHP have optional parameters. This means that a default value is set for them in advance, and you can omit such a parameter when calling the function.
Let's look at the built-in round() function, which rounds a number:
<?php
$result = round(10.25, 0); // 10We passed two values to it:
- The number that needs to be rounded.
- The rounding precision;
0means that the number will be rounded to an integer.
Since most often you need exactly rounding to an integer, the creators of the round() function made the second parameter optional and gave it the default value 0. That's why the result will be the same even if you don't specify the second parameter:
<?php
$result = round(10.25); // 10If you need a different precision, you can specify it explicitly:
<?php
// rounding to one decimal place
$result = round(10.25, 1); // 10.3round(10.25, 1) → arguments: 10.25, 1 → 10.3
round(10.25) → arguments: 10.25, (0) → 10
└── default valueThe number of optional parameters depends on the specific function, but required ones always come before optional ones.
A function's signature
Every function has a signature that contains a description of its name, its parameters, and the order in which they are used. The signature helps you understand what data the function expects and what will happen if the parameters are not specified.
Let's look at the documentation for the round() function. If we simplify it a little, the signature looks like this:
round(int|float $num, int $precision = 0): floatThe function is called round. The $num parameter is required — it takes the number to round (an integer or a fraction). The $precision parameter has the default value 0, that is, it is optional; if you don't specify it, the number will be rounded to an integer. After the colon comes the type of the result — the function returns a fractional number (float).
The full signature from the documentation has one more optional parameter that controls the rounding mode. It is rarely needed, so we won't consider it for now.
How to work with new functions
When you come across a new function, you can use a simple pattern:
- Open the documentation and find the function's signature.
- Look at usage examples.
- Go to the interactive PHP shell (it is launched with the
php -acommand) and try calling the function with different arguments.
This approach helps you quickly figure out how exactly a function works, which of its parameters are required and which are optional, and what results it returns.
Instructions
The code contains data about a car trip: the distance, fuel consumption, fuel price, and the number of passengers.
Calculate and print three values to the screen:
- The fuel volume in liters needed for the trip. Round it to one decimal place.
- The total cost of the trip in rubles. Round it to two decimal places.
- The trip cost for each passenger in rubles. Round it to an integer by calling
round()without the second argument.
Each value is printed on a separate line.
distance ──────┐
├──→ fuel ──────┐
fuelConsumption ───┘ ├──→ tripCost ──────┐
│ ├──→ perPerson
fuelPrice ─────────────────────┘ │
│
passengers ────────────────────────────────────────┘Tips
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.
Your exercise will be checked with these tests:
<?php
namespace HexletBasics\CallingFunctions\DefaultParams;
use HexletBasics\Exercise\TestCase;
class SolutionTest extends TestCase
{
public function test()
{
$expected = "37.8\n2426.76\n607\n";
$this->assertOutput($expected);
}
}Teacher's solution will be available in:
20:00
