Summing string in php, converted to integer -
trying utilize simple "versioning" scheme hashes, following:
$last_version = '009'; $increment = '001'; $result = $last_version + $increment; var_dump($result);
i expect: string(010)
int(10)
, before jump if
's , str-pad
, wondering if there's other way of conserving desired format?
using +
automatically casts variables appropriate number
type (in case int
, different string formats can casted float
).
if want maintain desired 0 left-padding, can utilize sprintf()
format result, such:
$result = sprintf('%03d', $last_version + $increment);
the format specifier %03d
specifies want integer-string (d
) length of 3
left-padded character 0
.
more info php's type juggling logic can found in php documentation: type juggling
php string integer sum
No comments:
Post a Comment