Jump to content

Recommended Posts

Let's say that I have a code that gets form input in the form of an equation as a string. How would I make my program do this equation?

 

<?php
$input = '2 + 2'; //$input is a string
$result = 2 + 2; //I'm not sure how to take the string and put it into equation form.
print($result);

 

 

Link to comment
https://forums.phpfreaks.com/topic/174395-string-to-equation/
Share on other sites

Please NOT eval(). OP has used $input suggesting that this comes from user input, so eval() would definitely be unsafe.

Such formulae need parsing. I'd recommend PHPExcel's calculation engine, but that may well be overkill; but I'm aware of at least one halfway decent formula parser in PHPClasses that would allow formulae typed in by a user to be safely parsed and evaluated.

Link to comment
https://forums.phpfreaks.com/topic/174395-string-to-equation/#findComment-919437
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.