Jump to content

what is the difference between php web and php command line?


lobster

Recommended Posts

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 ....

 

 

2zsqy3q.jpg

 

 

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>

 

<?php
if(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 ???

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

<?php
if(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 ... ??

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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 ...

Link to comment
Share on other sites

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;
?>
Link to comment
Share on other sites

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;
?>

 

 

<?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";
?>

 

you know a command line version of the quadratic equation solver .... ????

Link to comment
Share on other sites

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 by Barand
added example
Link to comment
Share on other sites

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";
?> 
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

 

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.

Link to comment
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.