DaniIvanov Posted October 9, 2012 Share Posted October 9, 2012 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 9, 2012 Share Posted October 9, 2012 Is this even possible with PHP or do I need Javascript or AJAX for it Yes it's possible, but how you do it kinda depends on what this variable is and how you're using it. Quote Link to comment Share on other sites More sharing options...
DaniIvanov Posted October 9, 2012 Author Share Posted October 9, 2012 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. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 9, 2012 Share Posted October 9, 2012 += assignment operator Use sleep() to make the script wait Quote Link to comment Share on other sites More sharing options...
DaniIvanov Posted October 9, 2012 Author Share Posted October 9, 2012 += 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. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 9, 2012 Share Posted October 9, 2012 (edited) Then don't put sleep at the beginning of the script. Increment first, then sleep(). Most likely it will go in a loop of some kind. Edited October 9, 2012 by TOA Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 9, 2012 Share Posted October 9, 2012 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. Quote Link to comment Share on other sites More sharing options...
DaniIvanov Posted October 9, 2012 Author Share Posted October 9, 2012 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? Quote Link to comment Share on other sites More sharing options...
TOA Posted October 9, 2012 Share Posted October 9, 2012 (edited) Definitely a cron job (Apache) or scheduled task(IIS) IMO Just write your script without the sleep call and set the script to run every minute Edited October 9, 2012 by TOA Quote Link to comment Share on other sites More sharing options...
codefossa Posted October 9, 2012 Share Posted October 9, 2012 I would do what TOA suggested, then with Javascript/Ajax you can get the amount of resources and other information on a set timer and display them. Quote Link to comment Share on other sites More sharing options...
DaniIvanov Posted October 10, 2012 Author Share Posted October 10, 2012 Thanks for all the replies.I will try the CronJob thing and see how far i can come. I dont know Javascript nor Ajax, perhaps i should learn one of those. They seem important Quote Link to comment Share on other sites More sharing options...
requinix Posted October 10, 2012 Share Posted October 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.