Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1099481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:44:34+00:00 2026-05-17T00:44:34+00:00

I have a small PHP script that is selecting a random sentence from an

  • 0

I have a small PHP script that is selecting a random sentence from an array

$prefixes = array('sentence 1', 'sentence 2');

echo $prefixes[rand(0,count($prefixes)-1)];

How can I make it not pick the same sentence twice in a row?

Edit: Sorry, yeah there will be around 100 sentences and they can be used over and over again just not twice in a row. It’s an on click event that re-loads the sentence to the page with AJAX and if it picks the same sentence twice in a row it looks like clicking the button hasn’t done anything as there is no page refresh. Thanks

Edit 2: Basically it’s a DIV with a sentence in, when you click a button it loads a new sentence into the DIV via AJAX and replaces the old sentence with the new one. Sometimes it was loading the same sentence twice in a row which gave the effect the button wasn’t doing anything

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:44:34+00:00Added an answer on May 17, 2026 at 12:44 am
    session_start();
    $prev = isset($_SESSION['last_number']) ? $_SESSION['last_number'] : null;
    do {
      $_SESSION['last_number'] = mt_rand(0,count($prefixes)-1);
    } while ($_SESSION['last_number'] === $prev);
    echo $prefixes[$_SESSION['last_number']];
    

    Of course, make sure you have more than one element in $prefixes, or else it will get stuck in an infinite loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a small php script, that displays random adverts on our site .
I have a small PHP script that takes the values from a dropdown list
I have this small PHP script that takes a POST variable and writes it
I have a problem with my index.php, i have this small script that decides
I have finalized a small PHP application that can serve many documents. These documents
So I have a small PHP script that generates an xml feel for the
I have a small PHP script that is listening for a POST request. I'm
I have this small PHP script: <?php session_start(); $var = array(); $var['key'] = 'Var
I have created a small php script locally that runs a java application in
I have a php script that echoes a large JSON array. This is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.