未初始化的字符串偏移量

时间:2012-03-13 02:12:34

标签: php

我在php中编写了一个类,它应该使用未知数字的公式(示例公式为x + y)并用用户输入的数字替换这些数字(x& y)(并传递给这个班)。我的班级看起来像这样:

                        <?php
        class formula
        {
            //declarations
                private $form="";
                private $variables="";
                private $values="";
            //end of declarations

            public function __construct($f, $vars, $vals)
            {
                $form=$f;
                $variables=$vars;
                $values=$vals;
            }

            public function getEquation()
            {
                //declarations
                $curChar="";
                $varCount=0;
                $charCount=0;
                $formulaChar=array();
                $i=0;
                $equation="";
                $size=strlen($this->form);  //number of characters in $form
                //end of declarations

                while($i<$size)
                {
                    $curChar=substr($this->form, $i);
                    $formulaChar[$i]=$curChar;
                    $i++;
                }

                while($charCount<$size)
                {
                    $varCount=0;
                    while($varCount<strlen($variables))
                    {
                        if($formulaChar[$charCount]==$variables[$varCount])
                        {
                            $formulaChar[$charCount]=$values[$varCount];
                        }
                        $varCount++;
                    }
                    $charCount++;
                }

                $charCount=0;

                while($charCount<count($formulaChar))
                {
                    $equation.=$formulaChar[$charCount];
                    $charCount++;
                }
                return $equation;
            }

        }
    ?>

出于测试目的,我创建了一个页面,将一些虚拟值传递给类,然后将结果写入屏幕:

        <?php
        include("formula.php");
        $formula="x+y";
        $variables=array("x", "y");
        $values=array(1,2);

        $test=new formula($formula, $variables, $values);
        echo $test->getEquation();

    ?>

当我运行它时,我得到一个错误,说我在第65行有一个未经初始化的字符串偏移量($ equation。= $ formulaChar [$ charCount];)。究竟是什么意思,我该如何解决?我猜我在阵列上做错了什么,但我并不是百分百肯定。

3 个答案:

答案 0 :(得分:1)

看起来你正在使用字符串。在PHP中,连接是用。,即

完成的
$equation .=$formulaChar[$charCount];

答案 1 :(得分:1)

如果您尝试添加$equation$formulaChar[$charCount],则$formulaChar[$charCount]似乎有字符串值。您应该将其投放到int

如下所示:

$equation+=(int)$formulaChar[$charCount];

如果$formulaChar[$charCount]是字符串,并且您希望与$equation连接,则使用“。”而不是“+”

$equation.=$formulaChar[$charCount];

编辑:

这是由于初始化不正常。您应该将$formulaChar初始化为array()而不是字符串

答案 2 :(得分:0)

给它一个机会。它将输入您的样本并返回:

1 + 2

<?php
class formula
{
    //declarations
    private $form="";
    private $variables="";
    private $values="";
    //end of declarations

    public function __construct($f, $vars, $vals)
    {
        $this->form=$f;
        $this->variables=$vars;
        $this->values=$vals;
    }

    public function getEquation()
    {
        $equation = $this->form;
        foreach ($this->variables as $index => $letter) {
            $equation = str_replace($letter, $this->values[$index], $equation);
        }

        return $equation;
    }

}

$formula="x+y";
$variables=array("x", "y");
$values=array(1,2);

$test=new formula($formula, $variables, $values);
echo $test->getEquation();
相关问题