0

Is it possible to increment a variable or array by 1 each time the script is executed.

An example would be, I execute the script and it sets a variable to $increment = '1';

I then close the script and come back 30 min later to execute the script again.

This time the variable needs to be $increment = '2';

The script will be run from cron every 30 minutes for a total of 8 times.

After the 8th execution the variable would need to be rewound back to $increment = '1';

Is this possible through session? Can session be setup for the cli sapi ?

Or would it be easier to output a text file with the next increment number and just pull the value from the text file?

All help is appreciated.

3
  • 1
    you can store the variable value in database,and on execution of the script fetch that variable and increments 1. Commented Aug 5, 2015 at 4:38
  • let's say I don't want to use database. I could store the value in an array instead right? Commented Aug 5, 2015 at 4:48
  • @Mike You need to store the values some where like cookie or file, if you don't want to use database. Commented Aug 5, 2015 at 4:52

2 Answers 2

2

You will have to persist the value of the variable to a non-volatile source between calls to the script. Furthermore, you'll have to consider race conditions if (as is often the case) the script can be called at almost the same time.

The best bet for this, is to store your variable in a database that maintains transactional safety. Even most cheap webhosters offer some form of database that will meet this requirement ... the specifics depend on your particular setup.

Sign up to request clarification or add additional context in comments.

5 Comments

the script will be called once every 30 minutes for 8 times total. My database can be an array couldn't it?
I just thought of this. I could also setup 8 variables in the script. var1, var2, etc and pull the value from those variables based on the server time of the current cron running. So, 8 total cron jobs. If cron job 1 starts at 1am, then variable 1 has an if statement that says use this value if current time is between 1am to 1:29am.
Sure, but only if you're sure about the timing -- but then, if you can functionally figure out the expected value based on the time, why are you putting yourself through this brain damage of any kind of variable? Just write a function to produce the value based on the current time.
you're right. I didn't see that far ahead yet. But yes, I'm sure of the timing. Each cron will be setup at a specific time.
this should work right? date_default_timezone_set('America/Utah'); // MDT $current_date = date('H:i:s'); if($current_date >= '00:00:00') { $increment = '1'; } elseif($current_date >= '00:30:00') { $increment = '2'; } elseif($current_date >= '01:00:00') { $increment = '3'; }
0

Store the counter value in database and on execution of the script update counter with + 1

Example script

$conn = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

if($query = $conn->query("YOU ACTION QUERY HERE"))
{
    // Here you call your actions of script

    /* Increment the counter field here. */
    $conn->query("UPDATE table SET counter = counter + 1");
}

$conn->close();

Note Its simple logic to increment variable by 1 each time script is executed.

4 Comments

thank you. I will keep this in mind for when I want to use database as the storage.
@Mike What you want to use for now?
I just need it to increment a value by 1 each time the script is executed. Example: website.com/file1.txt, then on next execution website.com/file2.txt.
I just saw this question, couldn't you just have an inc.txt file storing the current value and each time a script is run, just increment it & create a new file with that using the template file{$inc}.txt.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.