0
It is possible to do sum operations with sprintf
? In the example below, how to decrease, or add 1 in $idade
?
$idade = 10;
$str = '%d anos';
echo sprintf( $str , $idade );
0
It is possible to do sum operations with sprintf
? In the example below, how to decrease, or add 1 in $idade
?
$idade = 10;
$str = '%d anos';
echo sprintf( $str , $idade );
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);
That’s correct, but intended to make the function itself do the operation.
I think it’s also possible, but like, I don’t know more or less how you would want :/ to be received, and returned and given through a function.
Something like sprintf( '%d-1 anos' , 10 )
, to have an output of 9 anos
.
Must be the way you just wrote ?
Yeah, it should be the string '%d-1 anos'
Like, everything in quotes is just part of the template to be formatted, you can’t sign values from within (inside the quotes);
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
printf( $str , ++$idade );
Or do you need something more specific? at least it was the only way that it managed so far ... I tried to make the account in the expression but it did not work.– rray
@rray, is a translation package, wanted to do the operation in function, but I think there is no way. Its way is as I am currently using.
– Papa Charlie