PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
$number = 5;
echo "the number is ".$number;
echo "<br>";
square($number);
echo "the number squared is ".$number;
function square($number)
{
$counter = $number;
$onumber = $number;
do
{
$number= $onumber*$onumber;
$counter--;
} while ($counter >=1);
return $number;
}
?>
and the reply is
the number is 5the number squared is 5
... the function doesn't seem to work and i'm not sure why... it's probly a very stupid mistake that i've made... would appreciate the help though... thanx...
<?php
$number = 5;
echo "the number is ".$number;
echo "<br>";
$numberSquared = square($number);
echo "the number squared is ".$numberSquared;
function square($number)
{
$counter = $number;
$onumber = $number;
do
{
$number= $onumber*$onumber;
$counter--;
} while ($counter >=1);
return $number;
}
?>
in a loop, the square has already been worked out, you are just going to repeat the calculation as many times as the size of the number, which is a waste, image running the same calculation 1000 times!