0

I would like to execute a string as if it were PHP code.

An example would be:

$string='round(24.6,2)';

I would like to convert $string to executable syntax. Is there a way to do this?

1

2 Answers 2

2
eval() 

is the function you want. But: eval() is evil!

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

2 Comments

Saying eval() is evil is a stupid statement. I'm not saying you're wrong, however you really need an explination if you're going to put such a statament on SO.
@alrightgame that may be the case, but statements like that don't help people learn. SO provides valuable answers, but more importantly is a place for people to learn and become better at what they do. Simply stating that eval is evil will not make anyone who reads it better at what they do unless they can understand the reason behind such a statement.
2

You can use eval('round(24.6,2)'), but this is usually frowned upon for multiple reasons. Why do you want to do this?

4 Comments

Care to elaborate on these "reasons"? It would help the OP to understand why the problem should or shouldn't be tackled in a different manner.
@JonTaylor: It's inefficient and dangerous. Especially if you (the original author) didn't write this code, but some user.
@MadaraUchiha I understand the risks myself, I was simply trying to get the people answering to give a bit more detail in their answers, to help others.
@JonTaylor: The PHP manual has a perfect explanation of why it's not a good idea, and that was linked to in a comment - php.net/manual/en/function.eval.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.