1

My php code is:

<?php

class Markov {
protected $arr0;
protected $arr1;
protected $arr2;
public $n;
public $p00;
public $p01;
public $p02;
public $p10;
public $p11;
public $p12;
public $p20;
public $p21;
public $p22;
public function __construct($p)
{
    $arr0=new SplFixedArray(3);
    $arr1=new SplFixedArray(3);
    $arr2=new SplFixedArray(3);
    $this->n=$p;
 for($i=0; $i<3; $i++)
 {
     $arr0[$i]=0;
     $arr1[$i]=0;
     $arr2[$i]=0;
 }
}
public function calculate_constants($first,$last)
{
if($this->n>1)
{
 for($j=0; $j<count($first); $j++)
 {
     if($first[$j]>=0 && $first[$j]<3)
     {
       if($last[$j]>=0 && $last[$j]<3)
       {
           $this->arr.$first[$j][$last[$j]]+=1;

       }
     }
 }
}
    else
    {
        for($k=0; $k<3; $k++)
        {
          $this->p0.$k=$this->arr0[$k]/($this->arr0[0]+$this->arr0[1]+$this->arr0[2]);
        }
        for($k=0; $k<3; $k++)
        {
            $this->p1.$k=$this->arr1[$k]/($this->arr1[0]+$this->arr1[1]+$this->arr1[2]);
        }
        for($k=0; $k<3; $k++)
        {
            $this->p2.$k=$this->arr2[$k]/($this->arr2[0]+$this->arr2[1]+$this->arr2[2]);
        }
    }
    $this->n-=1;
}
   public function display_constants()
   {
   echo $this->p00.'\n'.$this->p01.'\n'.$this->p02;
   echo $this->p10.'\n'.$this->p11.'\n'.$this->p12;
   echo $this->p20.'\n'.$this->p21.'\n'.$this->p22;
   }
   }
   $m= new Markov(5);
   $m->calculate_constants(array(0,1,2),array(1,2,1));
   $m->display_constants();
   ?>

In this my variable values p00,p01 etc are not changing even though I have used it in functions to calculate their value. Please Help me to correct the code so that the required output can be obtained

3 Answers 3

1

I don't know what you actually want but you will not have any error now. One issue is that you haven't assigned values in array $arr0 but you are using it, I tried assign in for loop for getting ride from errors.

<?php
class Markov {
    protected $arr0;
    protected $arr1;
    protected $arr2;
    public $n;
    public $p00;
    public $p01;
    public $p02;
    public $p10;
    public $p11;
    public $p12;
    public $p20;
    public $p21;
    public $p22;
    public function __construct($p)
    {
        $this->arr0=new SplFixedArray(3);
        $this->arr1=new SplFixedArray(3);
        $this->arr2=new SplFixedArray(3);
        $this->n = $p;
        for($i=0; $i<3; $i++)
        {
            $this->arr0[$i]=0;
            $this->arr1[$i]=0;
            $this->arr2[$i]=0;
        }
    }
    public function calculate_constants($first,$last)
    {
        if($this->n > 1){
            for($j=0; $j<count($first); $j++){
                if($first[$j]>=0 && $first[$j]<3){
                    if($last[$j]>=0 && $last[$j]<3){
                        $var = 'arr'.$first[$j];
                        $this->{$var}[$last[$j]] += 1;
                    }
                }
            }
        } else {
            for($k=0; $k<3; $k++) {
                $this->{'p0'.$k}=$this->arr0[$k]/($this->arr0[0]+$this->arr0[1]+$this->arr0[2]);
            }
            for($k=0; $k<3; $k++) {
                $this->{'p1'.$k}=$this->arr1[$k]/($this->arr1[0]+$this->arr1[1]+$this->arr1[2]);
            }
            for($k=0; $k<3; $k++) {
                $this->{'p2'.$k}=$this->arr2[$k]/($this->arr2[0]+$this->arr2[1]+$this->arr2[2]);
            }
        }
        $this->n -= 1;
    }
    public function display_constants() {
        echo $this->p00.'\n'.$this->p01.'\n'.$this->p02;
        echo $this->p10.'\n'.$this->p11.'\n'.$this->p12;
        echo $this->p20.'\n'.$this->p21.'\n'.$this->p22;
   }
}
$m= new Markov(5);
$m->calculate_constants(array(0,1,2),array(1,2,1));
$m->display_constants();
?>

As your else condition is not executing, $p00, $p01....etc always be blank. You can move for loop to other function:

public function display_constants() {
        for($k=0; $k<3; $k++) {
            $this->{'p0'.$k}=$this->arr0[$k]/($this->arr0[0]+$this->arr0[1]+$this->arr0[2]);
        }
        for($k=0; $k<3; $k++) {
            $this->{'p1'.$k}=$this->arr1[$k]/($this->arr1[0]+$this->arr1[1]+$this->arr1[2]);
        }
        for($k=0; $k<3; $k++) {
            $this->{'p2'.$k}=$this->arr2[$k]/($this->arr2[0]+$this->arr2[1]+$this->arr2[2]);
        }
        echo $this->p00.'<br>'.$this->p01.'<br>'.$this->p02;
        echo $this->p10.'<br>'.$this->p11.'<br>'.$this->p12;
        echo $this->p20.'<br>'.$this->p21.'<br>'.$this->p22;
   }
Sign up to request clarification or add additional context in comments.

4 Comments

New Error coming: Notice: Undefined offset: 1 in C:\Wnmp\html\MarkovChain\Markov.php on line 42 Notice: Undefined offset: 2 in C:\Wnmp\html\MarkovChain\Markov.php on line 42 Notice: Undefined offset: 1 in C:\Wnmp\html\MarkovChain\Markov.php on line 42 \n\n\n\n\n\n
Line must be $this->{$var}[$last[$j]] += 1; So if you are trying to access $arr0[1] but your array have $arr[0] only then you will get this error. You have to sort this out according your need. What you actually wants to do?
I have initialised arr0[0],arr0[1] and arr0[2] with 0 value
First of all when you call $m->calculate_constants(array(0,1,2),array(1,2,1)); only if condition runs not else because $this->n is 5 so how you will get any value in $p00,$p01....etc. It will be blank always.
1

Try adding the following on top of your file (below the <?php tag)

error_reporting(-1);
ini_set('display_errors', true);

Blank page usually means an error, but those are hidden by default.

5 Comments

I am getting the following warnings and errors: Notice: Undefined property: Markov::$arr in C:\Wnmp\html\MarkovChain\Markov.php on line 47 Warning: Cannot use a scalar value as an array in C:\Wnmp\html\MarkovChain\Markov.php on line 47 Notice: Undefined property: Markov::$arr in C:\Wnmp\html\MarkovChain\Markov.php on line 47 Warning: Cannot use a scalar value as an array in C:\Wnmp\html\MarkovChain\Markov.php on line 47 Notice: Undefined property: Markov::$arr in C:\Wnmp\html\MarkovChain\Markov.php on line 47
at line 47 the value of the array which is getting computed is undefined
No, it is defined, but it is wrongly defined. It first tells you "Cannot use a scalar value as an array in xxxx" and then throws a notice. Its undefined because of the warning.
New error coming up: Notice: Undefined offset: 1 in C:\Wnmp\html\MarkovChain\Markov.php on line 42 Notice: Undefined offset: 2 in C:\Wnmp\html\MarkovChain\Markov.php on line 42 Notice: Undefined offset: 1 in C:\Wnmp\html\MarkovChain\Markov.php on line 42 \n\n\n\n\n\n
Please sign this as an answer if it solved your question :)
1

Please set error_reporting(-1); and ini_set('display_errors', true); in the top of your file.

1 Comment

I wouldn't put that in your php.ini file ;P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.