I have written a class in php that is supposed to intake a formula with unknown numbers (an example formula would be x+y) and replace those numbers (x & y) with numbers inputted by the user (and passed to this class). My class looks like this:
<?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;
}
}
?>
For testing purposes, I created a page that passed some dummy values to the class, and then wrote the results to the screen:
<?php
include("formula.php");
$formula="x+y";
$variables=array("x", "y");
$values=array(1,2);
$test=new formula($formula, $variables, $values);
echo $test->getEquation();
?>
When I run it however, I get an error saying I have an uninitalized string offset on line 65 ($equation.=$formulaChar[$charCount];). What exactly does that mean, and how do I fix it? I am guessing that I did something incorrectly with the array, but I am not 100 percent sure.
it looks like you are working with strings. in PHP, concatenation is done with the ., i.e.