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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:22:59+00:00 2026-06-15T00:22:59+00:00

I am testing a php function i found on the php documentation based on

  • 0

I am testing a php function i found on the php documentation based on this comment:
http://www.php.net/manual/en/function.rand.php#108861

<?php

    function RandNumber($e) {

        for ($i = 0; $i < $e; $i++) {
            $rand =  $rand .  rand(0, 9);  
        }
        return $rand;
    }

    echo RandNumber(4);
    // Outputs a 6 digit random number
?>

I get the error Notice:

Undefined variable: rand in /var/www/eod.php on line 7

This is line 7:

$rand =  $rand . rand(0, 9);

Why is causing this error since the function works as expected?

  • 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-06-15T00:23:04+00:00Added an answer on June 15, 2026 at 12:23 am

    Initialize your $rand variable to remove this warning :

     function RandNumber($e){
         $rand="";
         for($i=0;$i<$e;$i++){
            $rand =  $rand.rand(0, 9);  
         }
         return $rand;
     }
    

    Note that you may simplify your code :

     function RandNumber($e){
         $rand="";
         for(;$e-->0;){ // no need for an additional variable
            $rand .= rand(0, 9); // addition and assignement with one operator  
         }
         return $rand;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I followed this link http://www.phpunit.de/manual/3.6/en/database.html there i found some useful details of php db
<script type=text/javascript> function testing() { $.ajax({ type: POST, url: testing.php, data: call=+$(#abc).val(), success: function(msg){
this question is like this one , except it's for PHP testing via browser.
I think I may have found a bug in PHP's crypt() function under Windows.
Possible Duplicate: Find url from string with php I found this code: but 2
I searched, and found this question, which helped me: php static variable is not
Possible Duplicate: HTTP 404 - File not found Internet Explorer V6 I was testing
This I have found on the net in various places but with no actual
I've just been testing some PHP files with simpletest and found out thatit won't
I have these codes: testing.php <html> <head> <script type=text/javascript> function copy_data(id){ var 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.