Jump to content

Is there a way to stop loop inside switch?


HardCoreMore

Recommended Posts

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

 

See the PHP online documentation for break

 

So in your case it would be:

for( $i = 0; $i < 10; $i++ )
{
switch( $i )
{
	case 1:
	break;

	case 2:
	// break from loop here
	break 2;

	case 3:
	break;
}
}

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

 

See the PHP online documentation for break

 

So in your case it would be:

for( $i = 0; $i < 10; $i++ )
{
switch( $i )
{
	case 1:
	break;

	case 2:
	// break from loop here
	break 2;

	case 3:
	break;
}
}

 

 

Thanks!

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

 

See the PHP online documentation for break

 

So in your case it would be:

for( $i = 0; $i < 10; $i++ )
{
switch( $i )
{
	case 1:
	break;

	case 2:
	// break from loop here
	break 2;

	case 3:
	break;
}
}

 

Nice recommendation, I always used break to break out of loops, but never knew about the optional parameter!

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.