Nette Documentation Preview

syntax
Helper Functions
****************

.[perex]{data-version:3.1}
[api:Nette\Utils\Helpers] is a static class with useful functions.

→ [Installation and requirements |@home#Installation]

All examples assume the following class alias is defined:

```php
use Nette\Utils\Helpers;
```


capture(callable $cb): string .[method]
---------------------------------------

Executes a callback and returns the captured output as a string.

```php
$res = Helpers::capture(function () use ($template) {
	$template->render();
});
```


clamp(int|float $value, int|float $min, int|float $max): int|float .[method]{data-version:3.2.6}
------------------------------------------------------------------------------------------------

Returns value clamped to the inclusive range of min and max.

```php
Helpers::clamp($level, 0, 255);
```


falseToNull(mixed $value): mixed .[method]
------------------------------------------

Converts `false` to `null`, does not change other values.

```php
Helpers::falseToNull(false); // null
Helpers::falseToNull(123);   // 123
```


getLastError(): string .[method]
--------------------------------

Returns the last occurred PHP error or an empty string if no error occurred. Unlike `error_get_last()`, it is nit affected by the PHP directive `html_errors` and always returns text, not HTML.

```php
Helpers::getLastError();
```


getSuggestion(string[] $possibilities, string $value): ?string .[method]
------------------------------------------------------------------------

Looks for a string from `$possibilities` that is most similar to `$value`, but not the same. Supports only 8-bit encodings.

It is useful if a certain option is not valid and we want to suggest the user a similar one (but different, so the same string is ignored). In this way, Nette creates messages `did you mean ...?`.

```php
$items = ['foo', 'bar', 'baz'];
Helpers::getSuggestion($items, 'fo');   // 'foo'
Helpers::getSuggestion($items, 'barr'); // 'bar'
Helpers::getSuggestion($items, 'baz');  // 'bar', ne 'baz'
```


{{composer: nette/utils}}

Helper Functions

Nette\Utils\Helpers is a static class with useful functions.

Installation and requirements

All examples assume the following class alias is defined:

use Nette\Utils\Helpers;

capture(callable $cb): string

Executes a callback and returns the captured output as a string.

$res = Helpers::capture(function () use ($template) {
	$template->render();
});

clamp(int|float $value, int|float $min, int|float $max): int|float

Returns value clamped to the inclusive range of min and max.

Helpers::clamp($level, 0, 255);

falseToNull(mixed $value): mixed

Converts false to null, does not change other values.

Helpers::falseToNull(false); // null
Helpers::falseToNull(123);   // 123

getLastError(): string

Returns the last occurred PHP error or an empty string if no error occurred. Unlike error_get_last(), it is nit affected by the PHP directive html_errors and always returns text, not HTML.

Helpers::getLastError();

getSuggestion(string[] $possibilities, string $value): ?string

Looks for a string from $possibilities that is most similar to $value, but not the same. Supports only 8-bit encodings.

It is useful if a certain option is not valid and we want to suggest the user a similar one (but different, so the same string is ignored). In this way, Nette creates messages did you mean ...?.

$items = ['foo', 'bar', 'baz'];
Helpers::getSuggestion($items, 'fo');   // 'foo'
Helpers::getSuggestion($items, 'barr'); // 'bar'
Helpers::getSuggestion($items, 'baz');  // 'bar', ne 'baz'