0

I have string:

'$a["m"]*$a["m"]' 

And I want to do, that $a["m"] turn to its value (for example 5) and my program do the operation (so it will be 5*5 so 25).

EDIT:

I know, I can do this like:

$a["m"].'*'.$a["m"] 

But I don't want to ;) I can't. It will be in a string some time and then I plan to calculate that (when all of variables will have values).

I'm making something like "formulas calculator". I write formula in string and now, when my program works, and all of needed data is set, I can calc this string. All of these formulas will be in database, so I get this as a string.

Sorry, I didn't tell exactly what is the problem.

8
  • See eval. Commented Sep 22, 2013 at 13:04
  • @MartyMcVry and why would you suggest such a bad function to use? Commented Sep 22, 2013 at 13:06
  • @DarylGill Because he wants to run code inside a string. I do know that there are other (better) approaches, but if used carefully, it shouldn't pose much of a threat. Commented Sep 22, 2013 at 13:08
  • @MartyMcVry That's the problem, "used carefully". Theres many safer alternatives to eval.. This is such a dangerous code if you do not know how to correctly use it, judging from the question.. I don't believe the OP will hold such knowledge Commented Sep 22, 2013 at 13:10
  • Yeah, I'm looking for something like eval(). But, is there something that only calculate Maths, not run PHP code? Commented Sep 22, 2013 at 13:14

3 Answers 3

3

That's what pow() does:

$a['m'] = pow($a['m'], 2); 

If your value is stored literally as a string, then you need eval():

$a["m"] = '5'; $str = '$a["m"]*$a["m"]'; $var = eval("\$result = $str;"); echo $result; 

Output:

25 

And to quote Rasmus Lerdorf, the creator of PHP:

If eval() is the answer, you're almost certainly asking the wrong question.

For your usage, I think it'd be better to use an API such as WolframAlpha.

Get started here: http://products.wolframalpha.com/api/libraries.html

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

2 Comments

Yeah, eval() is answer to my question. But I know it is quite dangerous to use. Is there something that only calculate Math and can't edit/run PHP code?
@Ludwik11: I don't think something like that exists. But why are your variabes stored as a string in the first place? If it contains just numbers, it should be safe to use eval() but if your script involves user input anywhere, then eval() is a bad idea.
1

You should look into concatenation.. The following example should do what you are looking for.

$Var = $a["m"].'*'.$a["m"]; 

8 Comments

See (so it will be 5*5 so 25). I don't think that's what the OP wants.
it is not as simple as you think ^^ I want to keep that (opertion) in a string and run it "in proper time" ;)
@Ludwik11 Define what you mean by "proper time"
@Ludwik11: Is the value stored as a string, actually?
@Ludwik11 I'm struggling to understand your angle, do you want to validate your array contents as a certain data type? or will they be manipulated later on in the code before the final calculation.. Because theres only so much that just one array key can hold before changing entirely
|
0

First double qoutes " then single '

$a =$a['m'].' * ' . $a['m']; 

As per your Updated question:

You want to display the total in a string.

echo "The product of two numbers is:" .$a['m']. '*' .$a['m'] "; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.