0

For example

$var = '10/2';

Is there a way for me to output the value 5 from that easily?

So something like this:

$foo = ($var) + 5;

I want $foo to have a value of 10?

Currently, the best way I know is to explode $var and then divide $var[0] by $var[1]. Is there a quicker way?

Another way of asking; Is there a way to tell the system to treat '10/2' as an equation instead of a string?

zanussi
  • 1,286
  • 2
  • 22
  • 29
T. Short
  • 3,481
  • 14
  • 30

3 Answers3

0

Could be you can use a solution like this

$foo = eval('return '.$var +5 .';');
print $foo;

eval require a line of code .. so you could build a valid code line

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • **EVAL IS PROFOUNDLY DANGEROUS!** Code that relies on eval() is next to impossible to debug, unable to take advantage of any features PHP provides to accelerate code execution (such as op-code caching), and aside from SQL injection is probably the biggest source of security failures in high level languages of all time. No good will come of advising people to use it – GordonM Mar 03 '17 at 17:06
  • @GordonM I agree with you, mine is a technical answer ... your comment makes it evident to 'OP .. the question is about if this is possible .. – ScaisEdge Mar 03 '17 at 17:08
  • While this is technically correct I can't in good conscience upvote an answer that encourages bad practice without a warning about the risks. – GordonM Mar 03 '17 at 17:10
  • @GordonM Correct ... and your WARNING seem well visible – ScaisEdge Mar 03 '17 at 17:11
0

eval() to either assign in place or return:

eval("\$foo = ($var) + 5;");

$foo = eval("return ($var) + 5;");
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • **EVAL IS PROFOUNDLY DANGEROUS!** Code that relies on eval() is next to impossible to debug, unable to take advantage of any features PHP provides to accelerate code execution (such as op-code caching), and aside from SQL injection is probably the biggest source of security failures in high level languages of all time. No good will come of advising people to use it – GordonM Mar 03 '17 at 17:05
  • **PROFOUNDLY** Is a little dramatic. – AbraCadaver Mar 03 '17 at 17:09
  • Not really, no. Profoundly is a perfectly sound description of just how dangerous eval() can be – GordonM Mar 03 '17 at 17:09
  • _"can be"_ yes. Eating _"can be"_ dangerous if you eat the wrong mushrooms. – AbraCadaver Mar 03 '17 at 17:15
  • Some things are more dangerous than others. Eating can be dangerous but as a rule it's less dangerous than juggling live hand grenades on a high wire suspended over a bear pit. – GordonM Mar 03 '17 at 17:21
0

First things first, EVAL IS HAZARDOUS AND SHOULD NEVER BE USED UNLESS YOU KNOW WHAT YOU'RE DOING! As an addendum to this, you don't know what you're doing. No matter how experienced you are as a programmer, when it comes to eval just assume you don't know what you're doing. Believe me, in the long run your sanity will thank you.

As for your problem, you're basically asking how to write an equation parser. Writing a full blown one that can handle all cases of valid input (and reliably identify invalid input) is a much bigger job than you might at first think, so if you really do need to parse string equations it may be better to look for a library that will do it for you because the chances are whoever wrote the library thought of a lot of stuff that you didn't, such as handling operator precedence and how parenthesis can modify it, how to pass strings in scientific notation, etc.

One of your comments suggests using PHP Math Parser. I've never personally used it, but I know the author by reputation well enough to believe it's a reliable library.

If your use case is always going to be as simple as your example then you can simply split the string and process the resulting fragments.

$parts = explode ("/", $var);
$foo = $parts[0] / $parts[1];
GordonM
  • 31,179
  • 15
  • 87
  • 129