• 0

[PHP] Set a dynamic Equation as a function


Question

I'm not sure how to do this in php, but what I want to be able to do is have a user input an equation such as "x^2 + 2x -1" in a text box. Then I want to be able to store that as something like function f(), essentially f(x).

Then I want to be able to call something like f(1) and get it to process the function entered, which was stored as f(), using 1 as the x value.

Does anyone have an idea of how to do this?

5 answers to this question

Recommended Posts

  • 0

Doing such a thing directly in PHP would likely be impractical for the application. I would look into passing such an argument to an external program and having its results returned to the user via PHP.

Is "Octave" available on your system?

break the input string apart and put it into operators that you could rewrite into an octave script using various regular expressions and string functions. That looks almost exactly like what octave will take as input anyway. Then execute octave with the user parameter (i.e., f(1)) and return the outputted results to the user.

Actually, I want to see if this works now.

You may also find something useful over in the Math section of PEAR.

  • 0

Note you should do some additional checks to the input string but you can do something like that with this code:

<?php
session_start();
$equation = $_SESSION['equation'];

if( !empty( $_POST['submit'] ) ) {
	switch( $_POST['submit'] ) {
	case 'store equation':
		$equation = $_POST['equation'];
		// save equation
		$_SESSION['equation'] = $equation;
		break;
	case 'calculate f(x)':
		$x = $_POST['x'];
		$param = $x;
		$equation2 = str_replace( "x", "\$param", $equation ); // replace every instance of x to a parameter
		$result = eval( $equation2 );
		break;
	}
}
?>
<?php if( !empty( $result ) ) echo "<h2>" . $result . "</h2>"; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php if( $equation == "" ) { ?>
<input type="text" name="equation" value="">
<input type="submit" name="submit" value="store equation">
<?php } else { ?>
<h3><?php echo $equation; ?></h3>
<input type="text" value="x" value="<?php echo $x; ?>">
<input type="submit" name="submit" value="calculate f(x)">
<?php } ?>
</form>

test with 2 * x + ( 1 + x ) or with any equation

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I have YT subscription and Spotify but can you find rarer tunes on YT?
    • If they added ads I would dump it in a heartbeat, that's the whole reason I got the subscription to get rid of ads after every 2 tunes
    • That's if your company makes use of that separation. My company just uses the Intune portal for the authentication but doesn't utilize the separation, it just locks down the normal outlook and other Microsoft apps so you have to enter pin or biometric every time you open the apps, even for the personal sides.
    • Make your PC faster with this outstanding 1TB NVMe Gen5 SSD deal by Sayan Sen If you are not quite happy with how fast your PC boots up or how quick and snappy things feel, then MSI may have just the right thing for you. The company's SPATIUM M560 NVMe SSD is currently on sale for a price of just $70 for the 1TB model making it an amazing deal (purchase link under the specs table down below). The drive is based on 218-layer 3D TLC NAND and that means it has quite good endurance, even being a 1TB model. As it is a PCIe Gen5 NVMe SSD it promises very fast delivery speeds, but it does get quite hot and thus, adding a decent heatspreader is compulsory or else it will throttle under sustained loads which is common for PCIe 5.0 drives. The one minor drawback of the model is the lack of DRAM cache on it, however, it makes use of HMB (host memory buffer) technology that can access system memory for metadata caching purposes which can improve random access times. The technical specifications of the MSI SPATIUM M560 1TB are given below: Specification Value Controller Phison PS5031-E31T NAND Flash 218-layer Kioxia BiCS8 Sequential Read Up to 10,200 MB/s Sequential Write Up to 8,400 MB/s Random Read (4 KB) Up to 1,300,000 IOPS Random Write (4 KB) Up to 1,400,000 IOPS Operating Temperature 0 °C – 70 °C Storage Temperature –40 °C – 85 °C Endurance (TBW) 600 TBW MTBF Up to 1,500,000 hours Get it at the link below (comes with a five-year warranty): MSI SPATIUM M560 PCIe 5.0 NVMe M.2 1TB Portable SSD, Black: $69.99 (Sold and Shipped by Amazon US) If you want a 2TB option, check this Corsair MP700 Elite for $170. This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • Week One Done
      Tayloravila earned a badge
      Week One Done
    • First Post
      Axl917 earned a badge
      First Post
    • Week One Done
      pirateshiptours earned a badge
      Week One Done
    • Week One Done
      Zojaji earned a badge
      Week One Done
    • First Post
      Soeaker4thedead earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      731
    2. 2
      ATLien_0
      184
    3. 3
      +FloatingFatMan
      151
    4. 4
      Xenon
      115
    5. 5
      wakjak
      113
  • Tell a friend

    Love Neowin? Tell a friend!