Sum operations with sprintf

0

Is it possible to do sum operations with sprintf ? In the example below, how do I decrease or add 1 in $idade ?

$idade = 10;
$str = '%d anos';
echo sprintf( $str , $idade );
    
asked by anonymous 16.10.2015 / 23:51

1 answer

0

Since sprintf is a formatting function, it would not be incorrect to do:

<?php

$a = 1;
$b = 4;
$str = '%d anos menos %d = %d';

echo sprintf($str, $b, $a, $b-$a);
?>

[Edit]

function somar($a, $b){
    return sprintf('%d anos menos %d = %d', $b, $a, $b-$a);
}

echo somar(1,10);
    
17.10.2015 / 00:58