PHP: Functions with a variable number of parameters
Some functions have a special feature: they accept a variable number of arguments. This isn't related to default values, as it was with round(). The point is that the amount of data passed isn't limited to a fixed number.
Let's look at the max() function. It finds the largest value among the passed data.
<?php
print_r(max(1, 10, 3)); // => 10
print_r(max(1, -3, 2, 3, 2)); // => 3Let's open the documentation and look at the signature of max():
max(mixed $value, mixed ...$values): mixedThis means:
- the function requires at least one value (
$value); - after that you can pass any number of additional values (
...$values) — a variable number of arguments is denoted by the ellipsis...; - the function will return the largest of those passed.
The mixed type in the signature means "any type": the function can compare not only numbers, but other values as well.
If there are several identical maximum values among the arguments, the first of them is returned.
<?php
print_r(max(5, 5, 2)); // => 5The min() function works in the same way, only it looks for the smallest value:
<?php
print_r(min(1, 10, 3)); // => 1
print_r(min(1, -3, 2, 3, 2)); // => -3Instructions
Over the course of a week, the following air temperatures were recorded: 3, 10, 22, -3, 0 degrees. Find the minimum value using the min() function and print it to the screen.
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: Functions with a variable number of parameters
Some functions have a special feature: they accept a variable number of arguments. This isn't related to default values, as it was with round(). The point is that the amount of data passed isn't limited to a fixed number.
Let's look at the max() function. It finds the largest value among the passed data.
<?php
print_r(max(1, 10, 3)); // => 10
print_r(max(1, -3, 2, 3, 2)); // => 3Let's open the documentation and look at the signature of max():
max(mixed $value, mixed ...$values): mixedThis means:
- the function requires at least one value (
$value); - after that you can pass any number of additional values (
...$values) — a variable number of arguments is denoted by the ellipsis...; - the function will return the largest of those passed.
The mixed type in the signature means "any type": the function can compare not only numbers, but other values as well.
If there are several identical maximum values among the arguments, the first of them is returned.
<?php
print_r(max(5, 5, 2)); // => 5The min() function works in the same way, only it looks for the smallest value:
<?php
print_r(min(1, 10, 3)); // => 1
print_r(min(1, -3, 2, 3, 2)); // => -3Instructions
Over the course of a week, the following air temperatures were recorded: 3, 10, 22, -3, 0 degrees. Find the minimum value using the min() function and print it to the screen.
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\VariadicParameters;
use HexletBasics\Exercise\TestCase;
class SolutionTest extends TestCase
{
public function test()
{
$expected = '-3';
$this->assertOutput($expected);
}
}Teacher's solution will be available in:
20:00
