lobster Posted September 27, 2015 Share Posted September 27, 2015 i dont know much about the difference between the two ? both needs a php interpretor i guess ...? i stumbled upon this online php interpretor .. while trying to solve a maths problem ... which i ended up trying to solve a quadratic equation .... what is this one actually ? can this interpret both command line program and web based php programs ?? the quadratic equation example i tried to solve in this php interpretor looks like this ... <form method="post" action="index.php"><input type="text" name="a" value="Enter 'a'" /><input type="text" name="b" value="Enter 'b'" /><input type="text" name="c" value="Enter 'c'" /><input type="submit" name='calc' value="Calculate" /></form> <?phpif(isset($_POST['calc'])) //Check if the form is submitted{//assign variables$a = $_POST['a'];$b = $_POST['b'];$c = $_POST['c'];//after assigning variables you can calculate your equation$d = $b*$b - (4*$a*$c);$x1 = (-$b + sqrt($d)) / (2 * $a);$x2 = (-$b - sqrt($d)) / (2 * $a);echo "x<sub>1</sub> = {$x1} and x<sub>2</sub> = {$x2}";}else{//here you can put your HTML form}?> this one i cannot get to work with the above php interpretor due to some missing syntax i guess ... the second one looks like a command line version .... <?php$a = 1;$b = 2;$c = 1;$e = sqrt($b*$b-4*$a*$c);$f = ((-1)*$b - $e)/(2*$a);$d = ((-1)*$b + $e)/(2*$a);print "<br>";print "The Coefficients are<br>";print "a = ".$a."<br>";print "b = ".$b."<br>";print "c = ".$c."<br>";print "<br>";print "The equation is ".$a."x^2 + (".$b."x) + (".$c.")= 0<br>";print "The Solutions Are Given Below<br>";print "The first solution is " . $d . "<br>";print "The second solution is " . $f . "<br>";$a = 10;$b = 200;$c = 1;$e = sqrt($b*$b-4*$a*$c);$f = ((-1)*$b - $e)/(2*$a);$d = ((-1)*$b + $e)/(2*$a);print "<br>";print "The Coefficients are<br>";print "a = ".$a."<br>";print "b = ".$b."<br>";print "c = ".$c."<br>";print "<br>";print "The equation is ".$a."x^2 + (".$b."x) + (".$c.")= 0<br>";print "The Solutions Are Given Below<br>";print "The first solution is " . $d . "<br>";print "The second solution is " . $f . "<br>";?> this one works with the above php interpretor .. please help to make both of it work ??? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 27, 2015 Share Posted September 27, 2015 The biggest difference between the command-line interface (CLI) and the web-based interfaces is that the CLI doesn't have access to $_POST, $_GET etc. So if you want to use external values, you need to pass them as command-line arguments. They're then available in $argv. I don't know which interface the above website uses (appearently the CLI). If you want to work with PHP, I strongly recommend you install it locally. There are many tools like XAMPP which are easy to install and already contain a webserver, a database system etc. Quote Link to comment Share on other sites More sharing options...
lobster Posted September 27, 2015 Author Share Posted September 27, 2015 i have installed WAMP server .. i hope i can atleast get this working in wamp ... it prompts the user for a value ... like ... enter a value ... i dont know how to do that with a command line version of php right now ... i guess i will stay with the web version of php ... <form method="post" action="index.php"><input type="text" name="a" value="Enter 'a'" /><input type="text" name="b" value="Enter 'b'" /><input type="text" name="c" value="Enter 'c'" /><input type="submit" name='calc' value="Calculate" /></form> <?phpif(isset($_POST['calc'])) //Check if the form is submitted{//assign variables$a = $_POST['a'];$b = $_POST['b'];$c = $_POST['c'];//after assigning variables you can calculate your equation$d = $b*$b - (4*$a*$c);$x1 = (-$b + sqrt($d)) / (2 * $a);$x2 = (-$b - sqrt($d)) / (2 * $a);echo "x<sub>1</sub> = {$x1} and x<sub>2</sub> = {$x2}";}else{//here you can put your HTML form}?> how do i make this work ? it needs some extra syntax to make it work ... but am not sure where to add or what to add to it to make it work ... ?? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 27, 2015 Share Posted September 27, 2015 What's the problem? The code is working just fine. If there's a problem on your machine, you need to tell us exactly what's happening so that we can help you. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 27, 2015 Share Posted September 27, 2015 (edited) it prompts the user for a value ... like ... enter a value ... i dont know how to do that with a command line version of php right now If you running the code through the terminal/command line you cannot use HTML. HTML is for the web browser only. The terminal/command line is plain text only. If you want PHP to ask for user to input in the termina/command line you can use readline or have the the user passing the values as arguments when executing the command how do i make this work ? it needs some extra syntax to make it work ... but am not sure where to add or what to add to it to make it work ... ?? What do you mean? That code runs perfectly fine for me. If you are using WAMP make sure you are saving your php files in wamps www directory (C:/[path to wamp]/www). Then open your browser and go to http://localhost/your-file.php to run the code. Edited September 27, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
lobster Posted September 27, 2015 Author Share Posted September 27, 2015 i cant get any result here ??? i dont know how many things i am doing wrong here ??? Quote Link to comment Share on other sites More sharing options...
lobster Posted September 27, 2015 Author Share Posted September 27, 2015 If you running the code through the terminal/command line you cannot use HTML. HTML is for the web browser only. The terminal/command line is plain text only. If you want PHP to ask for user to input in the termina/command line you can use readline or have the the user passing the values as arguments when executing the command What do you mean? That code runs perfectly fine for me. If you are using WAMP make sure you are saving your php files in wamps www directory (C:/[path to wamp]/www). Then open your browser and go to http://localhost/your-file.php to run the code. thanks ... i am running wamp ... and i have saved the file in c:/wamp/www as index.php i get the form display right ... but when i put some value in it ... its giving me errors ... Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 27, 2015 Share Posted September 27, 2015 Can you post the errors Quote Link to comment Share on other sites More sharing options...
lobster Posted September 27, 2015 Author Share Posted September 27, 2015 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 27, 2015 Share Posted September 27, 2015 Are you entering any numbers/values into the three text fields, if so what are they? If you are not entering any values then that is why you are getting that error message. Quote Link to comment Share on other sites More sharing options...
lobster Posted September 27, 2015 Author Share Posted September 27, 2015 its working now ...thanks ... its giving me results .... Quote Link to comment Share on other sites More sharing options...
lobster Posted September 27, 2015 Author Share Posted September 27, 2015 is it possible to get a command line version of this quadratic equation solver ?? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 27, 2015 Share Posted September 27, 2015 I should add to prevent the error, then you need to validate the values the user has entered are numbers and is not zero. If the values pass validation then perform the calculation. Something like <?php if(isset($_POST['calc'])) //Check if the form is submitted { // DO VALIDATION - make sure values are all numeric and are not zero! // - error message will be set if any value does not pass validation // loop through all values foreach($_POST['values'] as $value) { // check to see if value is not numeric or is zero if(!is_numeric($value) || $value == 0) { // set error message $error = 'Values entered must be numbers and cannot be zero'; break; // stop the foreach loop } } //assign variables $a = $_POST['values']['a']; $b = $_POST['values']['b']; $c = $_POST['values']['c']; $result = ''; // only do the calcuation if the $error variable has not been set if(!isset($error)) { //values pass validation you can calculate your equation $d = $b*$b - (4*$a*$c); $x1 = (-$b + sqrt($d)) / (2 * $a); $x2 = (-$b - sqrt($d)) / (2 * $a); $result = "x<sub>1</sub> = {$x1} and x<sub>2</sub> = {$x2}"; } } // echo error message if it is set if(isset($error)) echo "<b>$error</b>"; ?> <form method="post" action=""> Value A: <input type="text" name="values[a]" value="<?php echo isset($a) ? $a : '' ?>" placeholder="Enter Value 'a'" /><br /> Value B: <input type="text" name="values[b]" value="<?php echo isset($b) ? $b : '' ?>" placeholder="Enter Value 'b'" /><br /> Value C: <input type="text" name="values[c]" value="<?php echo isset($c) ? $c : '' ?>" placeholder="Enter Value 'c'" /> <input type="submit" name='calc' value="Calculate" /> </form> <?php // echo the result of the calculation echo $result; ?> Quote Link to comment Share on other sites More sharing options...
lobster Posted September 27, 2015 Author Share Posted September 27, 2015 thanks i will save that for future reference ... i was wondering if i could do the same with command line version instead of the web form version ... To be able to read input from the script’s user, you can use STDIN combined with fgets(), fread(), fscanf() or fgetc(). For example: <?php$temp = fopen ("php://stdin","r"); $line = fgets($temp);echo $line; ?> <?phpfwrite(STDOUT, "Please enter your namen"); // Read the input $name = fgets(STDIN); fwrite(STDOUT, “Hello $name”); // Exit correctly exit(0); ?> <?php echo "Please enter value 1 : "; fscanf(STDIN, "%d\n", $value1); // reads number from STDIN standard input echo "Please enter value 2 : "; fscanf(STDIN, "%d\n", $value2); echo "Answer : " .($value1 + $value2) . "\n"; ?> you know a command line version of the quadratic equation solver .... ???? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2015 Share Posted September 27, 2015 (edited) something like this if ($argc < 4) { exit("USAGE: >index.php a b c\n"); } list(,$a,$b,$c) = $argv; if ($a != 0) { //after assigning variables you can calculate your equation $d = $b*$b - (4*$a*$c); $x1 = (-$b + sqrt($d)) / (2 * $a); $x2 = (-$b - sqrt($d)) / (2 * $a); echo "x1 = {$x1} and x2 = {$x2}"; } else echo "'a' cannot be zero"; example usage >index.php 1 -5 6 outputs : x1 = 3 and x2 = 2 Edited September 27, 2015 by Barand added example Quote Link to comment Share on other sites More sharing options...
lobster Posted September 27, 2015 Author Share Posted September 27, 2015 is it possible to make a command line version of this quadratic equation solver ?? To be able to read input from the script’s user, you can use STDIN combined with fgets(), fread(), fscanf() or fgetc(). For example: <?php $temp = fopen ("php://stdin","r"); $line = fgets($temp); echo $line; ?> <?php fwrite(STDOUT, "Please enter your namen"); // Read the input $name = fgets(STDIN); fwrite(STDOUT, “Hello $name”); // Exit correctly exit(0); ?> <?php echo "Please enter value 1 : "; fscanf(STDIN, "%d\n", $value1); // reads number from STDIN standard input echo "Please enter value 2 : "; fscanf(STDIN, "%d\n", $value2); echo "Answer : " .($value1 + $value2) . "\n"; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2015 Share Posted September 27, 2015 is it possible to make a command line version of this quadratic equation solver ?? WTF do you think the code I posted in reply #15 is for? Quote Link to comment Share on other sites More sharing options...
lobster Posted September 27, 2015 Author Share Posted September 27, 2015 (edited) sorry i could not even identify it as a php cli code ... how would i compile this ? do i need only WAMP to compile this code ? if so .. how do i do that ? sorry i am a noob ... Edited September 27, 2015 by lobster Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2015 Share Posted September 27, 2015 PHP does not require compiling. If it isn't already, put the directory containing php.exe in your path directive. At the command prompt, change to the directory containing your "index.php" invoke php.exe and pass it your arguments, which will be index.php, a, b, and c For example, to solve x^2 - 5x + 6 >php index.php 1 -5 6 Quote Link to comment Share on other sites More sharing options...
lobster Posted September 28, 2015 Author Share Posted September 28, 2015 thanks ... i also found this website a bit helpful .. You can execute your scripts in one of two ways: through a web server where the output is a web browser, or through the command line where the output is the command line itself. Of the two, the former is much more popular, but the latter is steadily growing in popularity http://www.hackingwithphp.com/2/5/0/running-php-scripts Quote Link to comment Share on other sites More sharing options...
lobster Posted October 9, 2015 Author Share Posted October 9, 2015 (edited) can someone suggest a good book with web forms and php ? Edited October 9, 2015 by lobster Quote Link to comment Share on other sites More sharing options...
lobster Posted October 20, 2015 Author Share Posted October 20, 2015 why cant i get a form output in this window ? is this only command line ? or is it incapable of outputting a form ? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 20, 2015 Share Posted October 20, 2015 why cant i get a form output in this window ? is this only command line ? Yes, because its command line only. Command Line/Terminal is plain text only. As I mentioned earlier to have that code display the form you need to run your code from PHP enabled HTTP server. You then browser to your php file in your browser to run the code. Quote Link to comment Share on other sites More sharing options...
lobster Posted October 20, 2015 Author Share Posted October 20, 2015 thanks ... i wish i could edit the thread title to "what is the difference between php web and php command line " ?? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.