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
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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.