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 6971177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:48:22+00:00 2026-05-27T16:48:22+00:00

Hit refresh several times and see sometimes I get null. This script loops through

  • 0

Hit refresh several times and see sometimes I get “null”.

This script loops through a folder to get all mp3 files and randomly selects one.
What am I doing wrong? Thanks

if ($handle = opendir('../../hope/upload/php/files/')) {

    while (false !== ($entry = readdir($handle))) {

        $entry = trim($entry);
        if(preg_match('/.mp3/', $entry))
        {
        $mp3[] =  "$entry";
        }
    }


    closedir($handle);
    $count = count($mp3);

$rand = rand(0,$count -1); /// FIXED BY adding a -1 after count**
$mp3 = $mp3[$rand];

if($mp3)
     {
     echo "http://MyWebsite.com/hope/upload/php/files/$mp3";
     }
else
     {
    echo "null";
     }
}
  • 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-27T16:48:23+00:00Added an answer on May 27, 2026 at 4:48 pm

    This is happening because array indexes go from 0 to length - 1, but your script is generating a random index from 0 to length. The preferred way to fix this would be to use array_rand():

    $rand = array_rand($mp3);
    $mp3 = $mp3[$rand];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll hit refresh on my page 20 times and it will work, then a
The problem i have is i could DELETE but then when i hit refresh
I hit this error while my web application was trying to execute a SELECT
I have hit upon this problem about whether to use bignums in my language
I have django supported web application. when i hit refresh button on a page
How can I avoid to submit again the form when I hit refresh of
What would cause IIS to return a 404, only sometimes? If I hit a
I have this pixelpost photoblog and it has a theme with a java script
First of all, I want to be clear: I did manage to get Internet
Basically I have created a Blackjack app that uses several methods that get called

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.