Re: % operator
| From: | Steve Werby | Date: | Sun, 04 Feb 2001 16:51:38 +0000 |
| Subject: | Re: % operator | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-38111@lists.php.net to get a copy of this message | ||
"Dhaval Desai" <dhaval_adams@yahoo.com> wrote:
> what is the use of %
It returns a remainder. For example 7 % 2 = 1.
> Is it called modulo..what is it's use in PHP.?
There are plenty of situations where it's useful. For example, if you're
displaying data in rows and want to shade every other row:
if ( 1 == $row_count % 2 ) { echo "<tr><td>$data</td></tr>"; }
else { echo "<tr><td class='shade-1'>$data</td></tr>"; }
Or if you have a data set you want to display in an HTML table across
multiple rows, you'll probably find it useful for determining when to put
<tr> or </tr> as you loop through your data set and use it to calculate
whether you will need to fill cells at the end of the table with
non-breaking spaces. There are many other applications, but these two come
up pretty frequently in web applications.
--
Steve Werby
COO
24-7 Computer Services, LLC
Tel: 804.817.2470
http://www.247computing.com/