Jump to content
MakeWebGames

How do I Variable Question


Recommended Posts

Posted

I am trying to set a variable that goes down by 1 every time a link is clicked. I am unsure how to do the variable properly.

$start = 10;

$monsterhp = $start - $damage;

$hit = rand(1,2);

if ($hit = 1) {

$damage = 1;

echo "You hit the monster for 1 hp damage.

Hit Again";

}

else {

$damage = 0;

echo "It hit you instead.

Try Again"; }

Some help would be appreciated.

Posted

Re: How do I Variable Question

The value that you want decremented needs to be stored in some sort of memory. You can use a session var, or store this in a database or file.

I'd opt for the session var.

 

// is the session var set?
if (!isset($_SESSION['start']) {

   // it's not set, so initialize it
   $_SESSION['start'] = 10;

} else {

   // decrement the var by one
   $_SESSION['start']--;

}

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.   Paste as plain text instead

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