Page 1 of 1

problems with $_GET['var']...

Posted: Sat Apr 22, 2006 7:44 am
by The-Master
quadratic equation problem, i coded this QE calc...
(qe.php)

Code: Select all

<?php
// the vars... a, b, c...
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
// echo the quadratic equation...
echo $a . "x(2) + " . $b . "x + " . $c ;
echo "<br><br>";
$b_1="-$b";
$b_2=square($b);
$a_2=$a*2;
$x_1=$b_1 + root($b_2-(4*$a*$c));
$x_1=$x_1 / $a_2
$x_2=$b_1 - root($b_2-(4*$a*$c));
$x_2=$x_1 / $a_2
echo "X(1) = ";
echo $x_1;
echo "<br>";
echo "X(2) = ";
echo $x_2;
?>
when i enter the page "http://loc.../qe.php?a=1&b=4&c=-30" for example i get...

Code: Select all

Parse error: syntax error, unexpected T_VARIABLE in C:\...\Qe.php on line 4
what's the problem in the code?

Posted: Sat Apr 22, 2006 8:33 am
by Oren
Well, since I noticed that you didn't do any checks in your script I just wrote my own instead of searching your mistake :wink:

Code: Select all

<?php	

	$a = $_GET['a'];
	$b = $_GET['b'];
	$c = $_GET['c'];




	$d = ($b * $b) - (4 * $a * $c);



	if ($d < 0)
	{
		die('Delta < 0');
	}



	if ($a == 0)
	{
		$x = (-$c) / $b;


		echo $x;	
	}


	else
	{
		$x1 = (-$b + sqrt($d)) / (2 * $a);
		$x2 = (-$b - sqrt($d)) / (2 * $a);


		echo $x1 . '<br />';
		echo $x2;
	}

?>

Posted: Sat Apr 22, 2006 8:44 am
by The-Master
thanks for the help, but i still need to echo the whole progress of the QE...
you think this will work:

Code: Select all

<?php

        $a = $_GET['a'];
        $b = $_GET['b'];
        $c = $_GET['c'];




        $d = ($b * $b) - (4 * $a * $c);



        if ($d < 0)
        {
                die('Delta < 0');
        }



        if ($a == 0)
        {
                $x = (-$c) / $b;


                echo $x;       
        }


        else
        {
                $x1 = (-$b + sqrt($d)) / (2 * $a);
                $x2 = (-$b - sqrt($d)) / (2 * $a);
                
                $sq=sqrt($d);
                echo 'X1:<br>'; // the whole progress
                $line_one="(-$b + $sq)";
                $line_one_2="(-$b + $sq)";
                $line_two="(2 * $a)";
                echo $line_one . '<br>';
                echo '<hr width="30"><br>'; // a line to separate
                echo $line_two . '<br>';
                echo 'X2:<br>'; // the whole progress (x2)
                echo $line_one_2 . '<br>';
                echo '<hr width="30"><br>'; // a line to separate
                echo $line_two . '<br><br>';


                echo $x1 . '<br />';
                echo $x2;
        }
?>
i have to add some HTML tags like table to make the output look better...

fixed: tried the code works great

Posted: Sat Apr 22, 2006 8:48 am
by Oren
In my code you have x1 and x2, what's the problem then? Just use them and add your text that's all.

Posted: Sat Apr 22, 2006 8:54 am
by The-Master
but if i echo $x1 and 2 it shows the solution... i need the progress so i added (" ") and made the code show the progress in two lines... by the way nice of you to write the code and add the no "c" thing... x(x-2) and all that...

Posted: Sat Apr 22, 2006 2:07 pm
by Oren
No "c" thing? You are probably talking on the checks I guess.
I just gave you the right direction, but you sill need to validate all the GET info.