I have the value as $title =10
and I want to minus it with 01
.So my code is:
echo $minus = round(($title - 01),2);
I want the result $minus = 09
but this code not like this it still have the result $minus=9
. Anyone help me please,Thanks.
I have the value as $title =10
and I want to minus it with 01
.So my code is:
echo $minus = round(($title - 01),2);
I want the result $minus = 09
but this code not like this it still have the result $minus=9
. Anyone help me please,Thanks.
The problem is that PHP is not strongly typed, and round()
returns a number, which automatically has the leading zero stripped. Try:
$minus = "0" . round(($title - 01),2);
PHP is evaluating your 0-prefixed numbers to their base value -- 04 and 01 are 4 and 1 respectively.
If you want them to be output with a leading 0, try using a number formatter, or string padding or simply append them to the string, "0"
What's happening is that round() returns an integer. Which means it won't have any 0's before it. If you want it to return 0's before it, try
str_pad(round(($title - 1), 2), 2, "0");
That will make it always append a 0 before the number if it's only 1 number, but if it's 10 or 15 or something, it won't append a 0
echo str_pad(intval($title) - 1, 2, "0", STR_PAD_LEFT);
This will pad your result with a 0 if the result is only one digit; otherwise, it will not pad. For a leading zero always, you can replace the 2 with strlen($title)
Try this..
$i=04-01;
echo sprintf('%02s', $i);