Jump to content

Recommended Posts

Hello PHP Freaks,

 

This is my first post on this forum. I have been learniing PHP for about two weeks now and I would like to know how to do the following.

 

I want to have a variable which every minute updates itself and adds some amount to the old one.

 

For example

 

$a = 1;
$b = 1.66;

// after 1 minute add $b to $a

 

Is this even possible with PHP or do I need Javascript or AJAX for it

 

Thanks in advance to anyone who replies.

Link to comment
https://forums.phpfreaks.com/topic/269248-autoupdate-variable-after-x-time/
Share on other sites

Yes it's possible, but how you do it kinda depends on what this variable is and how you're using it.

 

Hi requinix,

 

Maybe i dont understand what you mean with 'it depends on what this variable is and how you use it'. I am going to try and explain it better.

 

I want to have a variable which will contain either an integer or a float. Every 60 seconds PHP should update this variable and add an amount which i will specify in another variable to the first variable, sum both up, and save the result in the first variable.

 

The way I will use the variable is just by echo`ing it on the screen.

+= assignment operator

 

Use sleep() to make the script wait

 

 

Hi TOA,

 

 

Thanks for the reply

 

I dont want it to wait for 60 seconds and then load the page. I want it to load the page first and then auto-increment the variable every 60 second.

PHP can't change anything that's been sent to the browser already, so once you've done that the train has left the station. Only Javascript (or other client-side languages) can alter stuff on the client side.

 

However, you haven't told is why you want this and what you're going to use this number, which you're updating, for. You have just said what you wanted, as the solution you believe to be the correct one for your problem. Identify the problem instead, and we will be able to offer you a better solution.

Hello Christian,

 

I want to create an online game in which you have different resources which accumulate over time. Lets say i have gold, wood and food, and you gather X amount per minute. For example, you have a woodcutter building which produces 10 wood each minute

 

How do you suggest that I shall code this?

 

 

 

 

There's an alternative to cronjobs that can often work: calculating the expected values when they're needed. If X=10 as of an hour ago, and it increases at one per minute, now X=10+1*60=70. In fact the only time you have to update the stored value is when it changes due to some external factor.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.