Random PHP fun

Posted on

Decided to cook up a very quick & easy piece of code in PHP that prints from 1 to 30 and if any number in that range is divisible by 3 then print “Science”.

  for($i = 0; $i < 31; $i++) {
    if ($i == 0) continue;
    $science = ( ($i % 3) == 0) ? "Science" : $i;
    echo $science . "<br>";
  }

Result:

1
2
Science
4
5
Science
7
8
Science
10
11
Science
13
14
Science
16
17
Science
19
20
Science
22
23
Science
25
26
Science
28
29

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s