Basics: What did I do wrong?

I’m just learning PHP, so I’m experimenting with sort of random stuff. Now, I’m trying to get this code to work (as a power function).

The following does not output ‘81’ as expected, but rather ‘3’. The language I’m most familiar with is Java, but I’m rather new to coding in general.

Can anyone tell my what I did wrong?

Thanks so much!

[php]
function power($base, $power)
{
$result = 0;
if($power = 0)
{
$result = 1;
}
else
{
$result = $base;
for($i=0; $i<$power; $i++)
{
$result = $result * $base;
}
}
return $result;
}

echo power(3,4);
[/php]

Change if($power = 0) to if($power == 0)

Sponsor our Newsletter | Privacy Policy | Terms of Service