Jump to content

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

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.