Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

PHP Switch Statement Default Case w/array


  • Please log in to reply
7 replies to this topic

#1 RandomUser

RandomUser

  • Members
  • 518 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:12:03 AM

Posted 30 December 2009 - 08:10 PM

I want to know how to Break out of the Switch statement in the default case to tell a user if No Case Matches. I don't want to run the default case
when using an array for each variable in the array. I'd like to run the default case as a seperate peice of code only once.

Example:

<?php

	$in = array(1,2,3,4,5);

	foreach ($in as $a){

	switch ($a) {

	case 20:
	echo "20 is in the list";
	break;

	case 13:
	echo "2 is in the list";
	break;

	case 10:
	echo "10 is not the list";
	break;

	default:
	if ($a != array(1,2,3,4,5)) {
	echo "No Match Found<BR>";
}	

	}

}


?>

The issue here is that the Default case runs against each variable in the array, making it effectively redundant. If I use anything else,
the "Default" Case runs against each Variable. I want it to run only once and prompt a user to enter a valid response. I might change the
text to say, "No answer matches" please try again.

It has been suggested to use "do until" but I want to break out of the switch code if possible as I believe this would better suit the situation.

Thanks for you help in advance.

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:03 PM

Posted 30 December 2009 - 09:18 PM

I'm a bit confused what you mean. Either you said you wanted to remove the redundant IF statement in your switch:
<?php
	$in = array(1,2,3,4,5);
	foreach ($in as $a)
	{
		switch ($a)
		{
			case 20:
				echo '20 is in the list';
				break;
			case 13:
				echo '2 is in the list';
				break;
			case 10:
				echo '10 is not the list';
				break;
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
				break; /* Do nothing */
			default:
				echo 'No Match Found<br />';
		}
	}
?>

Or you mean you want to break out of the foreach loop, not the switch:
<?php
	$in = array(1,2,3,4,5);
	foreach ($in as $a)
	{
		switch ($a)
		{
			case 20:
				echo '20 is in the list';
				break;
			case 13:
				echo '2 is in the list';
				break;
			case 10:
				echo '10 is not the list';
				break;
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
				break; /* Do nothing */
			default:
				echo 'No Match Found<br />';
				break 2; /* Break out of the foreach */
		}
	}
?>

Does that make sense?

Billy3

PS: This is illegal in C but it's perfectly legal in PHP.

EDIT: Semicolon error.

Edited by Billy O'Neal, 30 December 2009 - 09:19 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:10:03 PM

Posted 30 December 2009 - 09:24 PM

Why are you checking again to see if the value is valid once it hits the default value? If it hits the default option, then it is not valid. No need to check it again. At any rate, I think there are better options, but it depends on what sort of input you are expecting. I think switch statements are clunky. Are you expecting the valid values to be sequential, within a range, or is there just some list of valid numbers?

#4 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:11:03 PM

Posted 30 December 2009 - 10:31 PM

He wants to break out of foreach loop once it is found that values do not match. But if you do not run the loop on all values then how would you find if values match or not? You need one or other kind of loop.

Instead of switch case, you may use a simple for loop like this:
<?php

	$in = {1,2,3,4,5}; //Input array
	$check = {10,20,30,40,50,60,70}; //values to check against
	$found = FALSE; //boolean to flag if values found or not
	foreach($in as $a){
		if(in_array($a,$check)){
			echo $a.' is found in the list.<br />';
			$found = TRUE;
		}
	}
	if($found==FALSE) echo 'No matches found';
?>

Edited by Romeo29, 30 December 2009 - 10:33 PM.


#5 RandomUser

RandomUser
  • Topic Starter

  • Members
  • 518 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:12:03 AM

Posted 30 December 2009 - 10:32 PM

As the code is in my original post, it will return "No Match Found" 5 times. I want it to only return that text once.

The application would be for a multiple choice question or something where one of the valid values is expected.

Thank You Groovicus, as I want to break out of the foreach loop. And that works.

Could tell me if break can be used in this way to break out of other loops and where multiple loops exist by just appending a
sequential number?
IE

break 2;

as you've done?

#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:03 PM

Posted 30 December 2009 - 10:40 PM

As the code is in my original post, it will return "No Match Found" 5 times. I want it to only return that text once.

The application would be for a multiple choice question or something where one of the valid values is expected.

Thank You Groovicus, as I want to break out of the foreach loop. And that works.

Could tell me if break can be used in this way to break out of other loops and where multiple loops exist by just appending a
sequential number?
IE

break 2;

as you've done?

1. My name is Bill.
2. See here.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#7 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:11:03 PM

Posted 30 December 2009 - 11:31 PM

If its a multiple choice question then why are you using foreach and switch case both?

If you are taking answers of many questions on a single page in an array and doing foreach on it, then you need to do more than a simple switch case. Plus you should not break out of foreach loop.

If the answers are pre-specified for each problem, then you can use a multi-dimensional array to check against.

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:10:03 PM

Posted 30 December 2009 - 11:32 PM

I am not explaining myself clearly. Too many lines of code today. I still is still not making sense to me why your default case has a loop in it in the first place. If I have a list of numbers, and none of them match my switch statements, then by default they do no exist. No need to check again.



At any rate, if I was doing this for a multiple choice type situation, I would have an array holding my valid values, and then check the entered values against them to see if that value exists in the array:
$arr = array(1, 2, 3, 4, 5);
$userNumber;

if (in_array($userNumber, $arr)){
   echo "The number is valid";
}else{
   "The number is not valid";
}

Or instead of allowing the user to input an incorrect number anyway, use a drop-down box that only has valid values. Then there is no opportunity for users to input an incorrect number; of course, we all know that users are stupid and malicious, so we still need to have server side validation just in case.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users