note 33897 deleted from control-structures.while by felipe
| From: | felipe@php.net | Date: | Tue, 29 Jan 2008 11:20:10 +0000 |
| Subject: | note 33897 deleted from control-structures.while by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-135514@lists.php.net to get a copy of this message | ||
Note Submitter: Merve
----
Here's something really cool and simple. Using the while loop, you can calculate exponents
(powers). The code looks like this:
<?php
//Since the values of $a and $b will be changing,
//we need to assign new variables to them, $c and $d.
$c = $a;
$d = $b;
while ($b>1)
{
//Here we're changing the value of $a.
$a = ($a*$c);
//Here we're changing the value of $b
$b--;
}
//And to top it all off, a little statement at the end.
print (" $c<sup>$d</sup> = $a");
?>
This code works because we are subtracting 1 from the value of $b each time through the loop. Since
we're saying that $a = ($a*$c), we are changing the value of $a but not the value of $c. We
need the variables $c and $d for the statement at the end because the values of $a and $b have
changed. This is a really simple trick that most of you could have figured out in a matter of
seconds, but it is a good trick for beginners like me.