Oihana PHP System

getParamBool.php

Table of Contents

Functions

getParamBool()  : bool|null
Retrieves a parameter from the HTTP request and converts it to a boolean.

Functions

getParamBool()

Retrieves a parameter from the HTTP request and converts it to a boolean.

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

This helper calls getParam() and converts the returned value to a PHP boolean according to standard boolean representations:

  • true/false (boolean)
  • "true" / "false" (string)
  • "1" / "0" (string)
  • "yes" / "no" (string)
  • "on" / "off" (string)
  • 1 / 0 (integer)

If the value cannot be interpreted as a boolean, the provided $defaultValue is returned. If $request is null or the parameter is missing, $defaultValue is returned unless $throwable is set to true, in which case a NotFoundException is thrown.

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 defaults passed to getParam().

$defaultValue : bool|null = null

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

$strategy : string|null = HttpParamStrategy::BOTH

Source to search: HttpParamStrategy::BOTH|QUERY|BODY. Default is BOTH.

$throwable : bool = false

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

Tags
throws
NotFoundException

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

example
// Query string: ?active=true
$active = getParamBool($request, 'active', [], false);

// Body parameters: ['enabled' => '0']
$enabled = getParamBool($request, 'enabled', [], true);

// Missing parameter with default fallback
$flag = getParamBool($request, 'flag', [], true);

// Throw exception if parameter missing
$required = getParamBool($request, 'required', [], null, HttpParamStrategy::BOTH, true);
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool|null

The boolean value of the parameter, or $defaultValue/null if not found or unrecognized.


        
On this page

Search results