Jump to content

[SOLVED] Loop in a Loop problems


NewbieBryan

Recommended Posts

I have two sets of code

 

This one calculates if a number is odd or even

<?php

// let's say you have a number
$number = 24;

// to test whether $number is odd you could use the arithmetic
// operator '%' (modulus) like this
if( $odd = $number%2 )
{
    // $odd == 1; the remainder of 25/2
    echo 'ODD Number!';
}
else
{
    // $odd == 0; nothing remains if e.g. $number is 48 instead,
    // as in 48 / 2
    echo 'EVEN Number!';
}
?>

 

This one adds 1 to a number until 5

<?php

$i=1;

while($i<=5)

  {

  echo "The number is " . $i . "<br />";



  	$i++;

  };


?>

 

I tried combining them but keep on getting errors; where am I messing up?

 

Erroneous code below

<?php

$i=1;

while($i<=5)

  {

  echo "The number is " . $i . "<br />";

	if( $odd = $i%2 )
	{
    echo 'ODD Number!';
	}
	else
	{
        echo 'EVEN Number!';


  	$i++;

  };


?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/165562-solved-loop-in-a-loop-problems/
Share on other sites

:D Apologies Crayon Violent, it is 03h00 here local time and my brain is fried.

 

The error is exactly that, I can't seem to be able to figure where to insert the last } ...

 

I have though on a couple of occasions managed to get the thing into an endless loop where is displays

 

1

ODD Number!

.

.

.

.

and repeat until I terminate the window.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.