Oihana PHP

toNumber.php

Table of Contents

Functions

toNumber()  : float|int|false
Converts a value to a numeric type (int or float) if possible.

Functions

toNumber()

Converts a value to a numeric type (int or float) if possible.

toNumber(mixed $value[, int|float|false $default = false ]) : float|int|false

Returns false if the value cannot be interpreted as a number.

This is useful to safely normalize user input or data that might contain numeric strings.

Parameters
$value : mixed

The value to convert.

$default : int|float|false = false

The default value if the value is not a numeric value.

Tags
example
use function oihana\core\toNumber;

echo toNumber('42');           // 42
echo toNumber('3.14');         // 3.14
echo toNumber('-0.5e2');       // -50.0
echo toNumber('foo');          // false
echo toNumber('foo', 0);       // 0
echo toNumber(null, -1);       // -1
author

Marc Alcaraz (ekameleon)

since
1.0.7
Return values
float|int|false

        
On this page

Search results