PHP Classes

Just some question

Recommend this page to a friend!

      PHP Equation Solver  >  All threads  >  Just some question  >  (Un) Subscribe thread alerts  
Subject:Just some question
Summary:fallback to string operations?
Author:Till Wehowski
Date:2015-04-24 15:09:15

  1. Just some question   Reply   Report abuse  
Till Wehowski - 2015-04-24 15:09:15
Hello Samuel,
I took a look at your class and have a first question.
First I have to say I am a deadbeat in math (really, unfortunattly), so maybe I could learn something here or reuse your class.

I did not have time to test it out, sorry maybe later, just took a look at the code.

The code is well commented and formatted, easy to read, also it is maybe usefull, so: Good - keep on!

The thing I do not get is about all the string operations.

$final_solution = $without_variable/$with_variable;
if (substr($final_solution, 0, 1) == "+")
$final_solution = substr_replace($final_solution, "-", 0, 1);
else if (substr($final_solution, 0, 1) == "-")
$final_solution = substr_replace($final_solution, "+", 0, 1);

Maybe I missed something you could explain, is a possitive value automatically signed in php really, so please sorry for that question?

$final_solution = $without_variable/$with_variable;
if (substr($final_solution, 0, 1) == "+")

Why do you not use

$final_solution = $without_variable/$with_variable;
if ($final_solution > 0)
$final_solution = $without_variable/$with_variable;
if ($final_solution >= 0)


  2. Re: Just some question   Reply   Report abuse  
Samuel Adeshina - 2015-04-24 21:12:42 - In reply to message 1 from Till Wehowski
Hi Tim, Thanks a lot for your question(s).

The logic behind this class' ability to solve equations is:
treating whatever equation that was supplied as a string, parsing it to verify if its actually a real equation and tries to identify the variable(s) and exponents used. After identifying the variable to solve for, it separates the parts of the equation with variables into an array and the ones without variables into another array called $with_variables and $without_variables respectively.
for instance, if you have the following equation:
3a + 2 = 1;

After parsing it you get two arrays looking like these:
[0=>'+3'] and [0=>'+2', 1=>'-1'].

It then adds up all the elements in each array together to get something like this:
$with_variables = (+2) + (-1) = +1;
$without_variables = +3
And then the final solution will be derived by dividing the $without_variables variable by the $with_variables variable as in
$final_solution = $without_variables / $with_variables
which gives us something like +3 / +1 = 3 in our previous example.

In order to return the final_solution with the appropriate sign ("+" or "-"), i had to extract the first index of the $final_solution variable then changing it to its opposite, using the if_else construct which you pointed out.

To answer your second question, the variable $final_solution needs to be treated as a string instead of as an integer (or any other number data type) in other to get the correct answer. For instance, using the example given above:
After initializing the $final_solution variable, we get +3 but the correct answer is suppose to be -3. That is why, the first index ('+' in this case) had to be extracted using the substr() function, then changing it to '-'.
I hope I was able to answer your questions or at least explain what was actually going on. You can always ask for further clarification.
Once again, thanks a lot for the question and the compliments.
With regards, Samuel