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.

8
  • 2
    if eval is the answer- the question is wrong (seriously eval is almost always a bad idea) Commented Jul 6, 2014 at 2:53
  • possible duplicate of Process mathematical equations in php Commented Jul 6, 2014 at 2:53
  • I saw that question, still unsure of how to use eval. Commented Jul 6, 2014 at 2:59
  • Are there any examples of it's use in this manner? Commented Jul 6, 2014 at 3:24
  • What do you mean when you say "do the following"? Do you mean the algebraic manipulations? Commented Jul 6, 2014 at 3:29

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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
$likes -= $dislikes; echo $likes;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.