PHP空白输出


PHP Blank output

我的php代码是:

<?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();
   ?>

在这个我的变量值p00,p01等没有改变,即使我已经在函数中使用它来计算它们的值。请帮我更正代码,以便可以获得所需的输出

我不知道你到底想要什么,但你现在不会有任何错误。一个问题是,你没有在数组$arr0中赋值,但你正在使用它,我试图在for循环中赋值,以避免错误。

<?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();
?>

如果你的else条件没有执行,$p00, $p01....等永远是空白。你可以将for循环移动到其他函数:

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;
   }

尝试在文件顶部(<?php标签下方)添加以下内容

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

空白页通常意味着错误,但默认情况下这些是隐藏的。

请将error_reporting(-1);ini_set('display_errors', true);设置在文件的顶部