1

Suppose I have a PHP script, somescript.php that runs every 5 minutes and takes 5 minutes to run each time. This script has a loop in it like:

for($x = 0; $x <= 100; $x++ )
{
$variable = "good";
...[rest of code]
}

Is it possible to change the value of $variable while somescript.php is actively running in a way that if before changing the value of $variable the loop iteration was at 50, whereas after the change, for iterations 51-100, they will use the new value of $variable (ideally within Ubuntu)?

I don't always want the value to change. Suppose after running the script, I realized $variable has the wrong value and want to change its value without stopping the script?

3
  • 1
    Not quite understand your requirement...can't it be solved by adding if ($x==50) $variable = "new"; ? Commented Jan 31, 2016 at 8:33
  • @slbteam08, I don't always want the value to change. Suppose after running the script, I realized $variable has the wrong value and want to change its value without stopping the script? Commented Jan 31, 2016 at 8:55
  • 1
    I think this cannot be done by variable inside your PHP script. Need to store the state in some external storage like a file or redis and check it during iterations. Commented Jan 31, 2016 at 9:00

1 Answer 1

1

If somescript.php runs over command line then you can pass in a new variable right into the command in the following way:

php /path/to/www/path/to/script.php new_var

Then you can access it in your somescript.php:

$new_var = $argv[0];
for($x = 0; $x <= 100; $x++ )
{
    $variable = "good";
    if ($x >= 50){
        $variable = $new_var;
    }
    ...[rest of code]
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I edited question to clarify. I don't always want the value to change. Suppose after running the script, I realized $variable has the wrong value and want to change its value without stopping the script?
that "realizing" must be drafted in written form. How does somescript.php knows about correct value? If it ends up and returns wrong value then you are going to rerun it passing in new variable anyway.

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.