Sunday, January 24, 2016

PHP ODD OR EVEN: Display Numbers Using A Loop and Identify If Odd or Even

This example uses PHP looping statement specifically the while loop but you can also use the different looping statements. The example below will display numbers from 1 to 10 and will use a conditional statement (if else) to identify if the number is odd or even.

Feel free to visit Odd or Even Number Code using Java here.

PHP Code

<?php
$i=1;
while($i<=10)
{
echo $i;
if ($i%2==0)
echo " even";
else
echo " odd";
$i++;
}
?>

Refer to the previous article about the while loop for explanation on how to use it. I'll just jump into the conditional statement if which contains the condition wherein we get the remainder of the $i. If the remainder is 0, then it will display even. Otherwise, it will display odd. Just add a break tag before the increment statement to insert new line after each value.

Output



















PHP ODD OR EVEN: Display Numbers Using A Loop and Identify If Odd or Even

PHP ODD OR EVEN: Display Numbers Using A Loop and Identify If Odd or Even Rating: 4.5 Diposkan Oleh: Emoblazz

0 comments:

Post a Comment