Skip to content

Instantly share code, notes, and snippets.

@dan0nchik
Created November 7, 2020 13:10
Show Gist options
  • Save dan0nchik/0ff37b0fa53a85e38d5e9f4eb94a6e3d to your computer and use it in GitHub Desktop.
Save dan0nchik/0ff37b0fa53a85e38d5e9f4eb94a6e3d to your computer and use it in GitHub Desktop.
<?php
function equation($a, $b, $c){
if ($a != 0){
$D = $b*$b - 4*$a*$c;
if($D < 0){
return "No roots";
}
if($D == 0){
return -$b/(2*$a);
}
if($D > 0) {
$x1 = (-$b + sqrt($D)) / (2*$a);
$x2 = (-$b - sqrt($D)) / (2*$a);
return array($x1, $x2);
}
}
else{
if($b==0) return "No roots";
return -$c*$b;
}
}
$res = equation(6,0,0);
if(gettype($res) == "array"){
foreach ($res as $i) echo "Root: ".$i."</br>";
}
else print $res;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment