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
https://forums.phpfreaks.com/topic/213793-whats-wrong-with-the-code/
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 />";
?>

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 />";
}
?>

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 />";
}
}
?>

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.