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

  • Home
  • SEARCH
  • 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 8239117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:02:15+00:00 2026-06-07T20:02:15+00:00

Program that tests the rand function is an example: <?php class number { function

  • 0

Program that tests the rand function is an example:

<?php 
        class number {
           function number() {
               $occurences=0;
           }
           public $occurences;
           public $value;
        }
        $draws = 5000;
        $numOfInts = 10;
        //$integers = array();
        $integers [] = new number(); 
        //initialising loop
        for($i=0;$i<=$numOfInts;$i++)
            $integers[$i]->$value = $i;  //debugger points here
        
        for($i=0;$i<$draws;$i++) {
            $integers[rand(0,numOfInts)]->$occurences++;               
        }
        foreach($integers as $int)
            printf("%5d %5d  <br/>",$int->$value,$int->$occurences);       
 ?>

Errors on the WAMP server:

Undefined variable: value in C:\path\index.php on line 31

Fatal error: Cannot access empty property in C:\path\index.php on line 31

What caused them and how to fix it? I suppose that the $integers is declared incorrectly.

  • 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-07T20:02:17+00:00Added an answer on June 7, 2026 at 8:02 pm

    You should access members of an object with this syntax:

    $integers[$i]->value
    $integers[$i]->occurences;
    

    However you have to initialize your array first as well which means un-comment the initial line to

    $integers = array();
    

    As a matter of fact you are not using the better OOP style which would change your data structure like this:

    class Number {
        private $value;
        private $occurences = 0;
        public function __construct($value = 0) {
            $this->value = $value;
        }
        public function getValue() {
            return $this->number;
        }
        public function addOccurence() {
            $this->occurences++;
        }
        public function getOccurences() {
            return $this->occurences;
        }
    }
    

    You would then access the members like this:

    // init part
    $integers = array();
    for($i = 0; $i < $numOfInts; $i++) {
        $integers[] = new Number($i);
    }
    
    // draws part
    for($i=0; $i < $draws; $i++) {
        $integers[rand(0,$numOfInts-1)]->addOccurence();               
    }
    
    // print part
    foreach($integers as $number) {
        printf("%5d %5d<br />", $number->getValue(), $number->getOccurences());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a program that I'm testing with Coded UI Tests, I've got a window
For my class, I am making a program that manages a Hotel. My 'Test'
After profiling a large game playing program, I have found that the library function
I have written a java program that tests the speed of a couple of
I recently wrote a program that takes inputted char data, tests if it is
I'm wiring a program that tests a set of wires for open or short
I'm having trouble building a simple c++ program that tests out regex's from the
So I have to do a program that basically tests that an XML text
So I am currently learning C++ and decided to make a program that tests
I am attempting to write a small recursive program that tests a list and

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.