Jump to content

random number


TheUnknown

Recommended Posts

<?php $k = rand(1,25);?>

 

then down the page it will echo

 

<?php echo $k ; ?>

 

a random number between 1 to 25

 

so every time the page loads the number will change.  I would not like the number to change every time

I was trying to figure out how i can get the number to only change lets say one time every 6 times the code is executed

Link to comment
https://forums.phpfreaks.com/topic/90115-random-number/#findComment-462080
Share on other sites

You need to store a counter somewhere, either in a database or as a session variable.

 

Here's an example using a session variable:

<?php
session_start();
$count = (isset($_SESSION['count']))?$_SESSION['count']:0;
$k = ($count == 6)?rand(1.25):$_SESSION['k'];
$_SESSION['count'] = $count + 1;
$_SESSION['k'] = $k
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/90115-random-number/#findComment-462106
Share on other sites

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.