PhpRiot
Follow phpriot on Twitter
Sponsored Link
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Add and subtract

Measurements can be added together using add() and subtracted using sub(). The result will use the same type as the originating object. Dynamic objects support a fluid style of programming, where complex sequences of operations can be nested without risk of side-effects altering the input objects.

Example 598. Adding units

<?php
// Define objects
$unit = new Zend_Measure_Length(200Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1Zend_Measure_Length::METER);

// Add $unit2 to $unit
$sum $unit->add($unit2);

echo 
$sum// outputs "300 cm"


Automatic conversion

Adding one object to another will automatically convert it to the correct unit. It is not necessary to call convertTo() before adding different units.

Example 599. Subtract

Subtraction of measurements works just like addition.

<?php
// Define objects
$unit = new Zend_Measure_Length(200Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1Zend_Measure_Length::METER);

// Subtract $unit2 from $unit
$sum $unit->sub($unit2);

echo 
$sum;


Zend Framework