Tell me how to display the result of the code? Where do I need to prescribe echo or something else and how to attach the button <p><input type="submit" value=Посчитать> </p> to make it work?

 <? function square_roots($a, $b, $c) { if ($a == 0) return false; if ($b == 0) { if ($c < 0) { $x1 = sqrt(abs($c / $a)); $x2 = sqrt(abs($c / $a)); } elseif ($c == 0) { $x1 = $x2 = 0; } else { $x1 = sqrt($c / $a) . 'i'; $x2 = -sqrt($c / $a) . 'i'; } } else { $d = $b * $b - 4 * $a * $c; if ($d > 0) { $x1 = (-$b + sqrt($d)) / 2 * $a; $x2 = (-$b - sqrt($d)) / 2 * $a; } elseif ($d == 0) { $x1 = $x2 = (-$b) / 2 * $a; } else { $x1 = -$b . '+' . sqrt(abs($d)) . 'i'; $x2 = -$b . '-' . sqrt(abs($d)) . 'i'; } } return array( $x1, $x2 ); } ?> 

-

 <html> <head> <title>Решение квадратного уравнения </title> </head> <body> <table cellspacing="0" style="width:100%"> <p>Введите a: <input type="text" name="val1" size=10></p> <p>Введите b: <input type="text" name="val2" size=10></p> <p>Введите c: <input type="text" name="val3" size=10></p> <p><input type="submit" value=Посчитать> </p> </table> </body> </html> 
  • one
    @ wolf123, To format a code, select it with the mouse and click on the button 101010 of the editor. - Nicolas Chabanovsky

2 answers 2

According to the logic of what is happening, like this:

 <?php /* здесь ваша функция */ $result = square_roots($a, $b, $c); # echo $result[0].' , '.$result[1]; ?> 

And with regards to the button, now there is no way to explain to you.

    About the button - there are lots of options

    1) <form action='index.php'></form>

    where index.php is the file where your function is located and called. We finish

    <input type='submit' value='Поехали!'> ,

    when you click on it, the parameters will be transferred to the script. The default is GET (when the data is visible in the address bar). But the method can be changed to POST. Further in the script we accept the data and voila. The result can be displayed as simple echo, or var_dump or print_r.

    I personally use

    echo "<pre style='text-align:left'>"; print_r($array); echo "</pre>"; получается симпатично

    2) You can hang on the submission button js function.

    <input type='button' onclick='foo()'>

    3) ajax