I am building a calculator program to help learn PHP. In the following example, I wrote a function to calculate exponents. When I run this function, It always takes the first case.
Any Ideas why that is?
Code:
function power_of($base, $exponent)
{
$result = $base;
switch ($exponent)
{
case $exponent = 0:
$result = 1;
break;
case $exponent > 1:
while ($exponent > 1)
{
$result = $result * $base;
$exponent--;
}
break;
case $exponent < -1:
while ($exponent < -1)
{
$result = $result * $base;
$exponent++;
}
$result = 1 / $result;
break;
}
return $result;
}
Comment