1

I want my variable $code to be incremented by 1 everytime, the php script is called. This is my code:

script.php:

<?php 
    $code = "05000"; /*On first run, the $code will be "05000", 
                       then on second run it will be "05001" and so on and so forth*/
?>

Or in other words, I want my $code to give a unique value (which should be self incrementing) everytime, the script is called. How can I do that?

3
  • 3
    You will need to use some kind of static storage for the current value, this could be saving it to file, or a database for example. Commented Jul 6, 2021 at 15:05
  • 1
    This might help Commented Jul 6, 2021 at 15:11
  • where do yo want to store the count? in a file? in the DB? Commented Jul 6, 2021 at 15:58

1 Answer 1

1

Store it in a simple file, you can use this ($count = $code for you) :

<?php

$storageFile = "storage.txt";

if (!file_exists($storageFile))
{
    file_put_contents($storageFile, "0");
}

$count = file_get_contents($storageFile);
file_put_contents($storageFile, ($count + 1));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.