Jump to content

referencing info from an If statement


severndigital

Recommended Posts

ok so I want to run a function, but I only want to run it once. even if i call the page again.

how do I run the function ONLY 1 time and then after re-calling the page reference the varibles values.

 

Here is what i have

 

function buryTreasure($h,$w){
//some math and data manipulation here
$per = 10;
          $res = array();

         for($i = 0; $i < $per; $i++){
                 $res[] = rand(0,100);
         }
return $res;
}

now i want to run that function ONCE to get my result. But how do make it so that it I can use the result of the function?

 

Thanks,

C

 

 

 

 

 

 

     

 

Link to comment
https://forums.phpfreaks.com/topic/120980-referencing-info-from-an-if-statement/
Share on other sites

once for 1 session? or once for all time?

 

If its just once per session, use a session variable.

 

<?php
// check if its been called or not
if($_SESSION['treasure'] == 0) {
buryTreasure(1,1);
$_SESSION['treasure'] = 1;
} else {
  echo "Function has already been called this session!";
}

 

or if its just once forever. Use a database to store 0 by default for the treasure. And then when its been called like above, update the database and set it to 1.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.