PHP/Javascript Eval Primer Tutorial

PPHP/Javascript Eval Primer Tutorial

PHP/Javascript Eval Primer Tutorial

Today’s tutorial adds an onion layer on top of an answer of a question on Yahoo Answers. This is the way I really like to program. Get the essence right first, then build onion layers on top, and have supervisors of supervisors of supervisors … for Onion a then Onion 1-a then Onion A-1-a … then Onion of the Eval Dimension … what! … yes, good old much maligned (because, yes, it needs to be used with caution for software security reasons), Eval! What is Eval? Starting to sound familiar with all the election talk? Eval is the construction of a line of code and the language calculating the answer on the fly, and both PHP and Javascript have their own “Eval”s, pretty obviously achieving completely different things (being as PHP is serverside and the Javascript am using is clientside), but both happening to be useful for the achievement of this tutorial, where we take a file with calculator type statements in it, like “56 * 23 / 234=” and constructing a program to answer the calculator type question. Well, did this in PHP/Javascript and then realized, as I slipped on a non-existant onion peel, that this method could be extended for a CSV input scenario. So the end result is a webpage that accepts via the URL with an ?infile= clause, the input data in either style of format, and the extra “get” parameter &interactive=y to say you want a Javascript alert box for each calculator result, rather than seeing all the results at once (also available are &leaveinquote=y and &justanswer=y (or n) and the &leavequote=y&justanswer=y would allow a comma-separated series of strings be concatenated and output to the screen if you choose the default Mathematical operators of (all) + signs).

Here is a link to some downloadable PHP programming source code which can be renamed to eval_js_php.php for your perusal.

Enjoy the live interactive tutorial (or one we baked before here, or one that was half-baked here or maybe you’d like some “sweet, green icing flowing down”), as we depart with Wikipedia’s view of Eval.

In some programming languages, eval is a function which evaluates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval. The input to eval is not necessarily a string; in languages that support syntactic abstractions (like Lisp), eval’s input will consist of abstract syntactic forms.

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Software, Tutorials and tagged , , , , , , , . Bookmark the permalink.

7 Responses to PHP/Javascript Eval Primer Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *