1

I am currently unable to figure out how I would do the following in php.. eval? But how?

The main problem? I'm not sure how to use mathematical variables like "X" and "Y".

If someone could show me how to solve the following equation in php, it would be awesome!

There are 30 students in a class, and 4 more girls then there are guys.

x = boys
y = girls

x + 4 = y
x + y = 30
(Substitution)
x + (x + 4) = 30
2x + 4 = 30
2x = 26

x = 13

y = 17

Thanks!

Just to clarify, I have taken a look at eval, and I have not found any sufficient examples on how to use it in this manner - If I had, I would not've asked this question. If anyone can point me to examples which use mathematical variables like this - that's also appreciated, but the question that was flagged as "might being an answer to my question", has not answered my question sufficiently.

Neither do the php docs on eval.

muffinjello
  • 158
  • 1
  • 5
  • 16
  • 2
    if eval is the answer- the question is wrong (seriously eval is almost always a bad idea) –  Jul 06 '14 at 02:53
  • possible duplicate of [Process mathematical equations in php](http://stackoverflow.com/questions/4019418/process-mathematical-equations-in-php) –  Jul 06 '14 at 02:53
  • I saw that question, still unsure of how to use eval. – muffinjello Jul 06 '14 at 02:59
  • Are there any examples of it's use in this manner? – muffinjello Jul 06 '14 at 03:24
  • What do you mean when you say "do the following"? Do you mean the algebraic manipulations? –  Jul 06 '14 at 03:29
  • Yessir. I've tried eval, but I'm using it wrong, and quite frankly, the php docs on it are pathetic and tiny - I learn from simple examples, and it does not have any examples like this. – muffinjello Jul 06 '14 at 03:31
  • @Dagon why link me to another question which the main answer - is to use eval, even when you yourself said that eval is a bad idea. – muffinjello Jul 06 '14 at 03:34
  • @muffinjello because life is strange and mysterious. –  Jul 06 '14 at 04:43

1 Answers1

1

There's no simple easy way to solve any given type of equation in PHP. You might also be looking for something more like MATLAB, as PHP wasn't exactly designed with this in mind.

You can write algorithms in any language to solve it, though. Here I've solved yours using randomness and brute force (probably the simplest and one of the least efficient methods):

<?php

while ($total != 30 || $girls != $boys + 4) {
    $girls = rand(0, 30);
    $boys = rand(0, 30);
    $total = $girls + $boys;
}

echo "There are {$girls} girls and {$boys} boys.";

In academic Computer Science, solutions to problems like these are called Algorithms, and graduates usually cite that course as their program's most important (along with Data Structures).

Adelmar
  • 2,073
  • 2
  • 20
  • 20
  • Yeahh... I'm looking at a 'like' system, where the amount of likes/dislikes overall is recorded, and the current like/dislike is consolidated into one (ie. +3, and if someone dislikes it, it becomes +2) This would be.... very brutal – muffinjello Jul 06 '14 at 04:30
  • $likes -= $dislikes; echo $likes; – Adelmar Jul 06 '14 at 04:47