Reputation: 393
I would like to post a value to my MySQL database in this format;
01, 02, 03...
011, 012, 013...
0101, 0102, 0103,
etc.
(with a 0
before each value).
If I do it like this "01+1"
I (understandable) get the value 2 and not "02".
is there a default php function that let's me set the default amount of characters or is there an other option?
I'm using laravel combined with eloquent and some own functions like so;
$lastSKU = $data->GetData('get', 'products', 'filter')->max('SKU');
$newValue = $lastSKU+1;
Thanks in advance.
Upvotes: 1
Views: 205
Reputation: 2489
It sounds like you won't know how the format will be, if there is always a 0 in front it is easy enough to do
$value = '0'. ( $value + 1 );
PHP will automatically convert it to a number between the brackets and by adding a string to it it will become a string again.
Now if you do not know the the length or the padding in advance but you do need to keep the current padding I would suggest something like
$value = str_pad(($value + 1), strlen($value), '0', STR_PAD_LEFT);
Edit, str_pad
is a bit slow these days, sprintf
can do the same trick but much faster
$value = sprintf("%'.0" . strlen($value) . "d\n", ($value +1));
Upvotes: 1