Jump to content

[SOLVED] isset() function - Easy question


Schlo_50

Recommended Posts

Hello guys,

 

Is it possible (and how would I do it?) to check to see if more than one variable is set and only use the ones that are set (TRUE) further down the script?

 

For example if my script checked to see if $var1, 2 and 3 were set and only $var1 and 3 were TRUE only those two would be printed.

 

(Also, can you have an if statement inside another if statement?)

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/108497-solved-isset-function-easy-question/
Share on other sites

as for the if statement inside another you could use if...else then there is also elseif...if and the switch statement.

 

i think if you use the switch statement you can accomplish what you need.  here is a link about the switch statement

 

http://uk2.php.net/switch

 

hope that helps.

Will I be able to use the switch statement to check and use vars that are only true and discard the false ones?

 

Basically I have a form, and it has 6 drop down menu's. If the user selects more than one option on the menu I want to be able to check to see if it's set and only use the ones that have been selected later on in the script. (Once i've found the TRUE variables I want to comma separate them into one string.)

the switch statement will evaluate all your case statements at once instead of working through them one by one as the if statement would do.  the link i posted gives some good examples of different ways of using this statement if you scroll down the page. 

 

 

Is this kind of on the right lines?

 

$relevant = "1";
$relevant_two = "2";
$relevant_three = "3";
$relevant_four = "4";
$relevant_five = "5";
$relevant_six = "6";

if (isset($relevant)) {
    $finala = $relevant;
} elseif (isset($relevant_two)) {
    $finalb = $relevant_two;
} elseif (isset($relevant_three)) {
    $finalc = $relevant_three;
} elseif (isset($relevant_four)) {
    $finald = $relevant_four;
} elseif (isset($relevant_five)) {
    $finale = $relevant_five;
} elseif (isset($relevant_six)) {
    $finalf = $relevant_six;
} 

switch ($relevant) {
case 0:
    print "$finala<br />";
    break;
case 1:
    print "$finalb<br />";
    break;
case 2:
    print "$finalc<br />";
    break;
case 3:
    print "$finald<br />";
    break;
case 4:
    print "$finale<br />";
    break;
case 5:
    print "$finalf<br />";
    break;
}

I've had another go, It doesn't quite do what I want it to still though. Perhaps someone could assist?

 

<?php
$relevant = "1";
$relevant_two = "2";
$relevant_three = "3";
$relevant_four = "4";
$relevant_five = "5";
$relevant_six = "6";


switch (isset($relevant) || isset($relevant_two) || isset($relevant_three) || isset($relevant_four) || isset($relevant_five) || isset($relevant_six)){
case "".isset($relevant)."":
	print "$relevant";
	break;
case "".isset($relevant_two)."":
	print "$relevant_two";
	break;
case "".isset($relevant_three)."":
	print "$relevant_three";
	break;	
case "".isset($relevant_four)."":
	print "$relevant_four";
	break;	
case "".isset($relevant_five)."":
	print "$relevant_five";
	break;
case "".isset($relevant_six)."":
	print "$relevant_six";
	break; 	 	
default:
	print "Nothing Selected.";
	break;
}

?>

 

Thanks

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.