Q:

php id with counter and save file

I want to make a counter whit a standard prefix before it: 202000 and then count 2020001, 2020002, 2020003 etc. And this data msut be saved in a file in or a database or in a txt file on my server, that doesn't matter. I tried much but I can come to it. I'm very new to PHP.
I have this code now, but i have a few issues so is only the last number saved, and is the prefix replaced by the counter, if you go above 10 than you get 2020010 and I want 20200010. Anyway here is my code:

    <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>

    <?php
    $file = 'counter.txt';

    // default the counter value to 1
    $before = uniqid('202000');
    $counter = 1;


    // add the previous counter value if the file exists
    if (file_exists($file)) {
        $counter += file_get_contents($file);
    }

    // write the new counter value to the file
    file_put_contents($file, $counter);
    echo $before + $counter;
    ?>


</body>
</html>

Can somebody help me?
Thanks Sam

A:

You don't have to save the actual prefix (202000) in your file. Just save the counter (1,2,3,4,5 etc) 
<?php
$file = 'counter.txt';
$prefix = '202000';

//If file exists then grab content of file(nr) and add it with one else set counter=1
file_exists($file) ? $counter = file_get_contents($file)+1 : $counter = 1;

//This is the number you would like to use (2020001, 2020002 ... 20200010 etc)
$use_number = $prefix .  $counter;  

//Save counter value to file (1,2,3,4 etc...)
file_put_contents($file, $counter);  
?>

