Oihana PHP System

getParamFloat.php

Table of Contents

Functions

getParamFloat()  : float|null
Retrieves a parameter from the HTTP request and ensures it is a float number.

Functions

getParamFloat()

Retrieves a parameter from the HTTP request and ensures it is a float number.

getParamFloat(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $args = [] ][, float|null $defaultValue = null ][, string $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : float|null

This helper calls getParam() and converts the returned value to a float if set.

  • If the value is null or missing, the $defaultValue is returned.
  • If $throwable is true, a NotFoundException may be thrown by getParam().
Parameters
$request : ServerRequestInterface|null

The PSR-7 server request instance.

$name : string

The parameter name or dot-notated path.

$args : array<string|int, mixed> = []

Optional default values passed to getParam().

$defaultValue : float|null = null

Value returned if the parameter is missing or not set. Default is null.

$strategy : string = HttpParamStrategy::BOTH

Which source to search: HttpParamStrategy::BOTH|QUERY|BODY. Default is BOTH.

$throwable : bool = false

Whether to throw a NotFoundException if parameter is missing. Default false.

Tags
throws
NotFoundException

If $throwable is true and the parameter is not found.

example
// Query string: ?price=19.95
$price = getParamFloat($request, 'price', [], 0.0); // 19.95

// Body: ['discount' => '5.5']
$discount = getParamFloat($request, 'discount', [], null); // 5.5

// Missing parameter, uses default
$tax = getParamFloat($request, 'tax', [], 1.0); // 1.0
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
float|null

The parameter value cast to float if present, otherwise $defaultValue or null.


        
On this page

Search results