• 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

    • Ashes of the Singularity II announced promising even larger battles, new faction, and more by Pulasthi Ariyasinghe A sequel for the 2016-released Ashes of the Singularity has officially been confirmed, and the original developer and publisher duo, Oxide Games and Stardock Entertainment, are back for this release too. Check out the debut trailer above. Ashes of the Singularity II is under development with a significantly expanded team compared to the original, according to today's announcement. It will be adding in a range of fresh units and gameplay systems for real-time strategy fans to dive into. More options for strategic gameplay, more intricate base building, and better enemy AI are listed as features for the new game as well. "Ten years ago, we set a new, literal benchmark for RTS games with massive battles and groundbreaking technology," said Brad Wardell, CEO of Stardock Entertainment. "With Ashes of the Singularity II, we’re raising the bar even higher, delivering strategic gameplay depth that RTS fans have always dreamed of.” The storyline of Ashes of the Singularity II will take place in 2031, with a complete single-player campaign, as well as cooperative and competitive multiplayer modes, being confirmed as features today. Moreover, a third faction is joining the fray this time, with Humans joining the fight alongside the original games' Post-Human Coalition and the Substrate. "Obviously, in the first game, the number one request was to have a human faction," adds Wardell. “Back then, we just couldn’t support having thousands of organic, walking, squishy people in the world and thus had to design in favor of machines. We’re really excited to bring the humans into the war and watch how they fare against the massive mechanical armies of the Substrate and PHC.” Ashes of the Singularity II is targeting a broad 2026 release window, with the team hoping to launch the title as the original celebrates its 10th anniversary. Its Steam store page is already up for wishlisting over here. Disclaimer: Neowin's relationship to Stardock
    • How exactly is a FREE image generation model a... Ponzi scheme?!?! I don't love all of these Gen-AI things flooding the market but there's nothing "ponzi" about it.
    • I can't decide if this is tacky or a really fun move for more casual players like myself to enjoy. I'll be curious to see how we are to fly to fictional land masses on a real globe.
  • Recent Achievements

    • One Month Later
      SamZrize earned a badge
      One Month Later
    • Week One Done
      SamZrize earned a badge
      Week One Done
    • One Year In
      SamZrize earned a badge
      One Year In
    • One Year In
      barracuda earned a badge
      One Year In
    • One Month Later
      barracuda earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      725
    2. 2
      +FloatingFatMan
      189
    3. 3
      ATLien_0
      180
    4. 4
      Xenon
      113
    5. 5
      neufuse
      109
  • Tell a friend

    Love Neowin? Tell a friend!