18.2. Creation of Measurements

When creating a measurement object, Zend_Measure methods expect the input/original value as the first parameter. This can be a numeric argument , a string without units, or a localized string with unit(s) specified. . The second parameter defines the type of the measurement. Both parameters are mandatory. As an optional third parameter, the language can be defined.

18.2.1. Measurements from variables

Voorbeeld 18.2. Creation using integer variables

<?php
require_once 'Zend.php';
Zend::loadClass('Zend_Measure');

$integer = 1234;
$unit = new Zend_Measure($integer,Zend_Measure::LENGTH);

echo $unit;
// outputs '1234 m'
?>
[Opmerking] Using floating point

Instead of integer types, floating point types may be used, but "simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a little loss of precision," sometimes giving surprising results. Also, do not compare two "float" type numbers for equality.

18.2.2. Measurements from strings

Many measurements received as input to ZF applications can only be passed to Zend_Measure classes as strings, such as numbers written using roman numerals or extremely large binary values that exceed the precision of PHP's native integer and float types. Since integers can be denoted using strings, if there is any risk of losing precision due to limitations of PHP's native integer and float types, using strings instead. Zend_Measure_Number uses the BCMath extension to support arbitrary precision, as shown in the example below, to avoid limitations in many PHP functions, such as bin2dec() .

Voorbeeld 18.3. Creation using strings

<?php
require_once 'Zend.php';
Zend::loadClass('Zend_Measure');
Zend::loadClass('Zend_Measure_Number');

$mystring = "10010100111010111010100001011011101010001";
$unit = new Zend_Measure($mystring,Zend_Measure_Number::BINARY);

echo $unit;
?>

Measurements can also be just a part of an arbitrary string. Usually, Zend_Measure can automatically extract the desired measurement. Only the first identifiable number will be used for measurement creation. If there are more numbers in the string, the rest will be ignored.

Voorbeeld 18.4. Arbitrary texts

<?php
require_once 'Zend.php';
Zend::loadClass('Zend_Measure');

$mystring = "My house is 125m² in size";
$unit = new Zend_Measure($mystring,Zend_Measure::AREA);

echo $unit;
?>

Voorbeeld 18.5. Arbitrary texts

<?php
require_once 'Zend.php';
Zend::loadClass('Zend_Measure');

$mystring = "My house is 125m² in size, it has 5 rooms of 25m² each.";
$unit = new Zend_Measure($mystring,Zend_Measure::AREA);

echo $unit;
?>

18.2.3. Measurements from localized strings

When a string is entered in a localized notation, the correct interpretation can not be determined without knowing the intended locale. The division of decimal digits with "." and grouping of thousands with "," is common in the English language, but not so in other languages. For example, the English number "1,234.50" would be interpreted as meaning "1.2345" in German. To deal with such problems, the locale-aware Zend_Measure family of classes offer the possibility to specify a language or region to disambiguate the input data and properly interpret the intended semantic value.

Voorbeeld 18.6. Localized string

<?php
require_once 'Zend.php';
Zend::loadClass('Zend_Measure');

$locale = new Zend_Locale('de');
$mystring = "1.234.567,89 Meter";
$unit = new Zend_Measure($mystring,Zend_Measure::LENGTH, $locale);

echo $unit;
?>