0

This question might sound very weird and ridiculing but I was wondering if its possible.

I want to manipulate the operators that are present in String in PHP. Eg:

$a = "3 + 2";

Is it possible to get the result as 5? Similary $a can also have another values like

$a = "3 - 2 "; or $a = "3 * 2";

In every cases, I want the appropriate result. But one operator will always be there in the String at the center. The requirement is, I want to know if the operator present in the String can be manipulated.

Thanks in advance

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
avishek gurung
  • 188
  • 2
  • 7

1 Answers1

0

You need to use eval. This is a function built into php.

http://php.net/manual/en/function.eval.php

Aashray
  • 2,753
  • 16
  • 22
  • 1
    You don't __need__ to use eval() and it's recommended that you don't, especially if the expression is user input – Mark Baker Aug 22 '13 at 07:20
  • Yeah I do know the ways you could go wrong with `eval` but just started php and don't know any other way to do it. It is one way though. – Aashray Aug 22 '13 at 07:25
  • Look at the link I posted for "How to make a calculator in PHP" to see a good safe alternative in ircmaxell's answer – Mark Baker Aug 22 '13 at 07:30