Incremental number in PHP

Asked

Viewed 157 times

0

I have a system that emits an XML. It was done in PHP. XML has a tag which is a random number, but has to be incremental.

I mean, in the tag has the number 1000. The next tag has to have the number 1001.

This is easy to do in PHP. I left a fixed variable and every time it clicks, it increments. Only all XML is coming with equal number as it is incrementing from a fixed variable.

How to make each XML have its incremental number?

  • You want random numbers in order (1000, 1005, 1154, 1874, 2109...) or numbers in order only (1000, 1001, 1002, 1003...)?

  • I don’t know if I understand this correctly, but you have to redeem the last saved value in order to increase it. Then put it in the loop (for or foreach). Another option, since this number can be random, is to create the first number based on the team()

  • I need numbers in order only 1000, 1001, 1002.

  • Yes, Andre! That way!

  • The entire xml is generated in a request or you want to work with XML as if it were a database?

1 answer

0


Good morning, I suggest you use a large number in the style of NF-e Batch, is a number with 15 digits, this way you generate it from the function microtime(), for example:

echo substr(str_replace(',', '', number_format(microtime(true)*1000000, 0)), 0, 15);

Credits are from the Nfephp project: https://github.com/nfephp-org/sped-common/blob/master/src/LotNumber/LotNumber.php

Note: in my understanding, it is either random or sequential, or use a random generation like mt_rand() ~or~ start at 1 or 0 and increase, I see as distinct concepts. If "has to be incremental" as you wrote, then it is not random.

  • Thanks was that, in case I can make a variave on top of that, like: $test = substr(str_replace(',', '', number_format(microtime(true)*1000000, 0)), 0, 15); ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.