Skip to content

NumberInfo

Mistralys edited this page Jun 18, 2021 · 9 revisions

The NumberInfo class is a handy tool to work with numeric values in HTML and CSS: it handles the most common units, and allows numeric operations like adding and subtracting values.

Usage

The easiest way is to use the parseNumber() function:

use function AppUtils\parseNumber;

$number = parseNumber('14px');

echo $number->getUnits();

This will output the following:

px

Numeric operations

Additions

use function AppUtils\parseNumber;

$numberA = parseNumber('40px');
$numberB = parseNumber(10);

$numberA->add($numberB);

echo $numberA->getNumber();

This will output the following result:

50

Conversions

A number can easily be converted to the notation to use either in an HTML or CSS context.

NOTE: Unitless numbers are assumed to be pixel values.

use function AppUtils\parseNumber;

$number = parseNumber('1.2em');

echo $number->toHTML();

New here?

Have a look at the overview for a list of all helper classes available in the package.

Table of contents

Find the current page in the collapsible "Pages" list above, and expand the page, to view a table of contents.

Clone this wiki locally