Sorry if my code is bad-written, I'm a beginner in this.
So, I have an array filled with 6 values :
$attacks= ['attack1', 'attack2', 'attack3', 'attack4', 'attack5', 'attack6'];
I also have a foreach statement with an isset statement made this way :
foreach ($attacks as $atkarray) {
if (isset($_POST[$atkarray])) {
In case you wonder, the if is working properly when the user select one of the values available.
Now, I have set a switch statement after the if to modify a value right after depending on the value :
switch ($_POST[$atkarray]) {
case 'attack4':
$attackmult = 3;
break;
case 'attack5':
$attackmult = 3;
break;
case 'attack6':
$attackmult = 6;
break;
default:
$attackmult = 1;
break;
}
Now the problem is that even if I chose "attack4" , "5" or "6" I will ALWAYS have the default value (1) and I frankly don't know why.
Thanks for the help !
Related
What if I want to make each case in the switch case been a equlity condition like this code example
switch (true){
case X==Y:
//do something
case X==Z:
//do something
case X has 0 :
//do something
}
Its working well if I want to check only one condition,By adding break
,But what if i want to check all cases and do only true cases
If you want to change flavor from if.....else and want to use switch....case
So you can try like this
$test = true;
$x=1;$y=1;$z=2;//uncomment any one and check
//$x=1;$y=2;$z=1;//uncomment any one and check
//$x=0;$y=1;$z=1;//uncomment any one and check
switch ($test){
case $x==$y:
echo "x==y";
break;
case $x==$z:
echo "x==z";
break;
case $x==0 :
echo "x has 0";
break;
default:
echo "he!he!he!he!he!he!";
}
uncomment any one and check all conditions
Check demo here : https://eval.in/907483
Here using $test = true; means if your condition goes true in case inside switch
so I have a variable called multiplier that contains a certain value depending on what the user registered with. What I am trying to write here is
If multiplier is equal to "sedentary" then give it the value of $sedmultiplier
If multiplier is equal to "lightly" then give it the value of $lightmultiplier
Im stuck on this, can't seem to figure out how this would be written.
switch ($multiplier==) {
case "sedentary":
$multiplier=$sedmultiplier;
break;
case "lightly":
$multiplier=$lightmultiplier;
break;
case "moderately":
$multiplier=$modmultiplier;
break;
case "very":
$multiplier=$verymultiplier;
break;
case "extremely":
$multiplier=$extrememultiplier;
break;
default:
multiplier==0;
}
Replace $multiplier== with $multiplier and your code should work.
Like this :
switch ($multiplier)
{
case "sedentary":
$multiplier=$sedmultiplier;
break;
case "lightly":
$multiplier=$lightmultiplier;
break;
case "moderately":
$multiplier=$modmultiplier;
break;
case "very":
$multiplier=$verymultiplier;
break;
case "extremely":
$multiplier=$extrememultiplier;
break;
default:
$multiplier==0;
}
Just like #jeroen suggested, I would set the multipliers as an array instead. This way you can reuse them and add/remove multipliers more easily.
// Create the array with name => multiplier
$multipliers = [
"sedentary" => $sedmultiplier,
"lightly" => $lightmultiplier,
"moderately" => $modmultiplier,
"very" => $verymultiplier,
"extremely" => $extrememultiplier,
// ... just add more here, if needed...
];
// Check if we have a key with the current name.
// If we do, return the value, otherwise return 0 as default.
$multiplier = array_key_exists($multiplier, $multipliers)
? $multipliers[$multiplier]
: 0;
I have the following if statement (except the final one would be much longer with more values) that I would like to condense.
if ($row['titleId'] == '123' || $row['titleId'] == '456' || $row[){
I would imagine it would end up something like:
if ($row['titleId'] == ('123'||'456'){}
Or would I be better off like this:
$array = ('123','456')
if (in_array($row['titleId'], $array){}
You can use switch for this:
switch ($row['titleId']) {
case '123': case '456': case '789': case '314': case '271':
doSomething();
}
I'd probably still prefer to have each case on a separate line but, if your goal is to reduce the "height" of your code, you can do it as above.
In terms of shortening the code in your comment, which apparently looks like this:
switch($row['titleId']){
case '8216': case '8678': case '8705': case '8216': case '8707':
$rows[$row['titleId']]=array();
break;
case '8214':
$rows['8216'][]=$row['titleId'];
break;
case '8791':
$rows['8678'][]=$row['titleId'];
break;
case '8643':
$rows['8705'][]=$row['titleId'];
break;
case '8666':
$rows['8707'][]=$row['titleId'];
break;
}
you could opt for something like:
$xlat = array('8214'=>'8216', '8791'=>'8678', '8643'=>'8705', '8666'=>'8707');
switch($row['titleId']){
case '8216': case '8678': case '8705': case '8216': case '8707':
$rows[$row['titleId']]=array(); break;
default:
if (array_key_exists($row['titleId'],$xlat)) {
$rows[$xlat[$row['titleId']]][]=$row['titleId'];
}
}
This compresses the common cases by putting them under the control of an associative array. Basically, if the title ID is in the array as a key, its lookup value will be used to affect the correct $rows entry.
I have a switch statement with 3 cases,like this:
switch($date) {
case 1:
echo "";
break;
case 2:
echo "";
break;
case 3:
echo'';
break;
default:
echo '';
break;
}
And i am wondering,if there is a way to loop through all cases if they are all true.But with using break,because if i am not using it,the cases wont work properly.So is there a way???
You shouldn't use switch if you want to see if multiple things are true about the variable in question since the switch statement will cut out once one of the cases holds true (i.e. it won't continue to look to see if the other cases also apply to the variable).
If your goal is to test if multiple things are true regarding a variable, just use an if statement:
if ($date == X && $date == Y && $date == Z) {
// Do something since all the conditions are met
}
Another possibility is to "fall through" your cases like this:
switch ($variable) {
case 0:
// Do something to (some) variable to indicate this case applies
case 1:
// Do something to (some) variable to indicate this case also applies
case 2:
// Do something to (some) variable to indicate this case also applies
echo "WHATEVER YOU WANT TO ECHO"
break;
}
I have multiple switch statements in on one of the pages in order to pass different variables to the URL, as well as different case. I need these different switch statements because I need the different variables.
However, when I put a "default" in one of the switch statements, that default applies to every other switch statement and so when I use the variable of another switch statement in the URL, the default case of that other switch statement will appear on screen, along with the case of this switch statement.
All of my switch statements have one or more cases and I really cannot figure out how to get around this. Please may somebody help me?
Thanks in advance,
Calum.
This might be way off, but I think you need something like this:
if (isset($_POST['myvar'])) {
switch ($_POST['myvar'] {
case 1:
....
break;
default:
....
break;
}
} else if (isset($_POST['myvar2'])) {
switch ($_POST['myvar2'] {
case 1:
....
break;
default:
....
break;
}
}
Does that make sense?
make sure that you have a "break;" statement at the end of each of your cases, and that the default case is the last one. like this:
switch ($var) {
case 1: // do stuff 1;
break;
case 3: // do stuff 2;
break;
// ...
default: // do default stuff
}