Jump to content

what's wrong with the code ?


runeveryday

Recommended Posts

<?php
for ($count = 0; $count < 10; $count++)
{
$randomNumber = rand(1,50);
if ($randomNumber < 10)
goto less;
else
echo "Number greater than 10: $randomNumber<br />";
}
less:
echo "Number less than 10: $randomNumber<br />";
?>

 

i checked this code again and again,but couldn't find any error. who can help me,thank you.

Link to comment
Share on other sites

I am not sure if it's this, (i never worked with goto outside actionscript)

I have the feeling you miss a {} after else

<?php
for ($count = 0; $count < 10; $count++)
{
$randomNumber = rand(1,50);
if ($randomNumber < 10)
goto less;
}else{
echo "Number greater than 10: $randomNumber<br />";
}
less:
echo "Number less than 10: $randomNumber<br />";
?>

Link to comment
Share on other sites

goto less; Will give an error, so will less:

 

You were also missing some braces

 

<?php

for ($count = 0; $count < 10; $count++)
{
$randomNumber = rand(1,50);
if ($randomNumber < 10){
//goto less;
}else{
echo "Number greater than 10: $randomNumber<br />";
}
//less:
echo "Number less than 10: $randomNumber<br />";
}
?>

Link to comment
Share on other sites

OMG, the goto madness has started.

 

DON'T use goto to make simple conditional logic complicated and hard to read and follow in your code -

<?php
for ($count = 0; $count < 10; $count++)
{
$randomNumber = rand(1,50);
if ($randomNumber < 10)
{
	echo "Number less than 10: $randomNumber<br />";
} else {
	echo "Number greater than or equal to 10: $randomNumber<br />";
}
}
?>

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.