Texan78 Posted December 8, 2014 Share Posted December 8, 2014 Hello, I am having a small issue with a case switch array that uses a range of data. I am using it to set the background of table cells depending on the data. I.E. 0-0.9, 1-1.9 etc with each one being a different color. Issue I am having is it doesn't change. It sets it based on the first array entry. What might I be missing? This is the first time I have used a case switch with a range. -Thanks $magScale = ''; switch($magnitude) { case myInterval >= 0 && myInterval <= 0.9: $magScale = 'rgb(195, 218, 236)'; break; case myInterval >= 1 && myInterval <= 1.9: $magScale = 'rgb(210, 238, 197)'; break; case myInterval >= 2 && myInterval <= 2.9: $magScale = 'rgb(244, 240, 202)'; break; case myInterval >= 3 && myInterval <= 3.9: $magScale = 'rgb(244, 223, 202)'; break; case myInterval >= 4 && myInterval <= 4.9: $magScale = 'rgb(240, 199, 205)'; break; case myInterval >= 5 && myInterval <= 10: $magScale = 'rgb(212, 195, 236)'; break; } Quote Link to comment Share on other sites More sharing options...
requinix Posted December 8, 2014 Share Posted December 8, 2014 A switch (just called "switch") doesn't really do ranges. It's a fancy way of doing a lot of if/else ifs. switch ($value) { case $condition1: // if ($value == $condition1) case $condition2: // else if ($value == $condition2) ...What you've written is if ($magnitude == (myInterval >= 0 && myInterval <= 0.9)) { $magScale = 'rgb(195, 218, 236)'; } else if ($magnitude == (myInterval >= 1 && myInterval <= 1.9)) { $magScale = 'rgb(210, 238, 197)'; // ...which doesn't make sense, least of all because of whatever the "myInterval" thing is. There is a way to do this with a switch but it looks weird so you might as well just use a regular if block. if (myInterval >= 0 && myInterval < 1) { $magScale = 'rgb(195, 218, 236)'; } else if (myInterval >= 1 && myInterval < 2) { $magScale = 'rgb(210, 238, 197)'; // ...Note that I switched the second condition to a strict less-than: I don't know your application or the values of myInterval but it's safer to make sure there's no possible value being missed out on, and with the current version if myInterval=0.95 then nothing will happen. Even if you're sure that "0.95" won't happen. Quote Link to comment Share on other sites More sharing options...
Texan78 Posted December 8, 2014 Author Share Posted December 8, 2014 (edited) Thanks for your reply and explanation. To be honest, I don't know what myInterval is ether. This is a small script I started over a year ago and just now revisited. So I am not sure why I had that but, the little I do know, I know it doesn't look right and I believe is what is causing my problems. If/else would make more sense. Is it possible to use less than and great than signs for the range in the switch array? This is what I have changed it to. Seems to be working ok. Would this be ok or correct? -Thanks $magScale = ''; switch($magnitude) { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=1.9: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <=2.9: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <=3.9: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <=4.9: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; } Edited December 8, 2014 by Texan78 Quote Link to comment Share on other sites More sharing options...
requinix Posted December 8, 2014 Share Posted December 8, 2014 (edited) That won't work either. if ($magnitude == ($magnitude >= 0 && $magnitude Seeing the pattern yet? what you put in the switch == each case Really. Forget the switch and go with a normal if. if ($magnitude >= 0 && $magnitude Edited December 8, 2014 by requinix Quote Link to comment Share on other sites More sharing options...
Texan78 Posted December 8, 2014 Author Share Posted December 8, 2014 Why won't it work? It appears to be working fine now with the example above I posted. I believe the myInterval was just a place holder for my variable to check. Once I replaced it with the variable with the data to check it works fine now. So what about it won't work if it is working? I do see the pattern as you're using if/else but, what about the is better than the above? For simplicity I really don't see much of a difference. -Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 8, 2014 Share Posted December 8, 2014 (edited) I think you are missing how a switch works. The value in the constructor is compared to the values in the case statements. So, with this: switch($magnitude) { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; // . . . The first case statement is checking if $magnitude (in the constructor) is equal to the result of ($magnitude >=0 && $magnitude <=0.9). That would be the same as the following if() condition if($magnitude == ($magnitude >=0 && $magnitude <=0.9)) { What you are wanting is for the case statement to be evaluated to see if it is true or not. So, you need to put TRUE into the switch constructor. You can do that like this: switch(TRUE) { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=1.9: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <=2.9: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <=3.9: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <=4.9: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; } FYI: There are gaps in the logic with the comparisons. For example, the first one is $magnitude >=0 && $magnitude <=0.9 And the second is $magnitude >=1 && $magnitude <=1.9 What if the value os >0.9 and less than 1? The logic should be changed to: switch($magnitude) { case $magnitude >=0 && $magnitude <1: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=2: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <=3: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <=4: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <=5: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; } Edited December 8, 2014 by Psycho 1 Quote Link to comment Share on other sites More sharing options...
mikosiko Posted December 8, 2014 Share Posted December 8, 2014 (edited) @Texan78 .... Try testing that code with $magnitude = 0 and see what do you get Edit: Psycho gave you the reasoning behind Edited December 8, 2014 by mikosiko Quote Link to comment Share on other sites More sharing options...
requinix Posted December 8, 2014 Share Posted December 8, 2014 Why won't it work? It appears to be working fine now with the example above I posted.It's working for the wrong reasons so it's teaching you the wrong way to use a switch. This time it works, sure, but next time it won't and you won't understand why because "it worked that other time". Quote Link to comment Share on other sites More sharing options...
Texan78 Posted December 10, 2014 Author Share Posted December 10, 2014 It's working for the wrong reasons so it's teaching you the wrong way to use a switch. This time it works, sure, but next time it won't and you won't understand why because "it worked that other time". Every switch I have done I have done it this way per the PHP site. That is why I am a little confused as to how it is wrong. I try to self teach myself but, it seems you ask 20 people for suggestions and more than half of them will all tell you something different. If it is working I don't see how it is wrong. if/else is just another way to script it. As for the logic $magnitude >=0 && $magnitude <=0.9 And the second is $magnitude >=1 && $magnitude <=1.9 If you make it a whole number then it will not know which color to use therefore it will be NULL. That is why the ranges are the way they are. This has been tested. less than and more than doesn't exempt the number. So I.E. if the number is 1.9 or less or great than 1 it will include that entire range. It doesn't exclude 1 or 1.9 from than range. If you have two switches using the same range then it would be NULL because it wouldn't know which one to inherit. Again, this as been tested. At them moment it is working correctly with the solution I had posted through numerous testing. In order to continue my learning to fully understand I have tried it with another instance I have. Setting it to TRUE does not work and setting whole numbers does not work. $LRH_departScale = ''; switch(TRUE) { case $LRH_calcDepart >=0 && $LRH_calcDepart <=-2: $LRH_departScale = 'rgb(195, 218, 236)'; break; case $LRH_calcDepart >=-2 && $LRH_calcDepart <=-4: $LRH_departScale = 'rgb(210, 238, 197)'; break; case $LRH_calcDepart >=-4 && $LRH_calcDepart <=-6: $LRH_departScale = 'rgb(244, 240, 202)'; break; case $LRH_calcDepart >=-6 && $LRH_calcDepart <=-8: $LRH_departScale = 'rgb(244, 223, 202)'; break; case $LRH_calcDepart >=-8 && $LRH_calcDepart <=-10: $LRH_departScale = 'rgb(240, 199, 205)'; break; case $LRH_calcDepart >=-10: $LRH_departScale = 'rgb(212, 195, 236)'; break; } Then again this could be how I am embedding it in the HTML as I have tried many numerous ways. <td bgcolor="<?php echo $LRH_departScale; ?>"> <?php echo $LRH_calcDepart; ?></td> Quote Link to comment Share on other sites More sharing options...
hansford Posted December 10, 2014 Share Posted December 10, 2014 Why are you arguing and resisting what these senior level members are telling you? You're logic makes no sense and they have pointed out why a switch is not the solution in this instance. They aren't out to steer you in the wrong direction - they wouldn't waste their valuable time if that were the case. They are here to help you. I have been programming for years and I still learn from them. Quote Link to comment Share on other sites More sharing options...
Texan78 Posted December 10, 2014 Author Share Posted December 10, 2014 I don't believe anyone is arguing. In fact, if you read my previous post you will see I tried the example. I clearly asked for a explanation as to why it won't work so I will know further how not to do to it when what I tried clearly does work which is what I have been using for a couple years which was taught to me from a "Senior" member months ago on this board and is in line with the PHP Manual that is so often referred to. So when some people are saying doing it this way and others are saying no that is wrong when it is members from the same board then you can see my dilemma. According to the PHP Manual a is/else statement is just another way to write a switch. It clearly states it in the PHP Manual. So how is it wrong? That is all I was asking. I even tried the example and it still doesn't work. Why do people have to be some demeaning and add nothing to the solution for those who are novice and just do it for fun as a way to relax. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 10, 2014 Share Posted December 10, 2014 i have a suggestion that doesn't require writing out and/or editing a bunch of lines of program logic. use a data driven design - // data arrays: 1st element - start/low (inclusive), 2nd element - end/high (exclusive), 3rd element - value (whatever you need, a literal value, an array, an object...) $d[] = array(0,1,'rgb(195, 218, 236)'); $d[] = array(1,2,'rgb(210, 238, 197)'); $d[] = array(2,3,'rgb(244, 240, 202)'); $d[] = array(3,4,'rgb(244, 223, 202)'); $d[] = array(4,5,'rgb(240, 199, 205)'); $d[] = array(5,21,'rgb(212, 195, 236)'); $magnitude = 4; // an input value for testing $magScale = ''; foreach($d as $e){ if($magnitude >= $e[0] && $magnitude < $e[1]){ $magScale = $e[2]; break; } } if($magScale == ''){ echo "no matching mag scale found"; } else { echo "For magnitude $magnitude, mag scale - $magScale"; } Quote Link to comment Share on other sites More sharing options...
requinix Posted December 10, 2014 Share Posted December 10, 2014 Every switch I have done I have done it this way per the PHP site.I'm confident that the official manual did not tell you to do ranges using a switch. I'm looking now and I don't see anything telling you to do that. In fact I had to go into the user comments until I found something to do with ranges: this guy from a few years ago. (There were other comments like that but were all rated $randomizer = rand(1,50); switch($randomizer) { case ($randomizer <= 20): $font_size = "11"; break;That code is wrong and I just downvoted the comment for it. It will always work, yes, but if I made a tiny change to it $randomizer = rand(0,50); switch($randomizer) { case ($randomizer <= 20): $font_size = "11"; break;then there's a 1/50 chance that it will fail, PHP will raise an undefined variable warning, and the CSS in the outputted HTML echo '<span style="font-size: ' .$font_size. ';">' .$link[$i]. '</span> ';will be invalid. actually it's a 1/51 chance The only change I made was going from rand(1,50) to rand(0,50). So what? I added one more number, that's all. And that's the problem: it's not obvious why the code will fail, it'll be hard to reproduce, and you the developer will tear your hair out trying to figure out why there's this weird bug on your site that people are complaining about. That is why I am a little confused as to how it is wrong. I try to self teach myself but, it seems you ask 20 people for suggestions and more than half of them will all tell you something different. If it is working I don't see how it is wrong.Just because you yourself cannot see the problem doesn't mean there isn't one. Using your code from a few posts ago, switch($magnitude) { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=1.9: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <=2.9: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <=3.9: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <=4.9: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; }set $magnitude = 0 and see what happens. Should be pale blue, right? Nope. Now forget for a moment that $magnitude "should not" ever be 0 and think about the fact that your code did something it wasn't supposed to. if/else is just another way to script it.And it's another way that would force you to get the logic right. If you tried that switch above as an if/else then I'm sure you'll see the behavior is different. If you have two switches using the same range then it would be NULL because it wouldn't know which one to inherit. Again, this as been tested.If you're saying what I think you're saying, No. That is flat-out wrong. If two cases are both true at once then one of them will win. However, because of the bug in (the old version? of) your switch, you may have gotten the result you did because none of the cases matched the switch condition. Now I phrased that last sentence very carefully so if you want to show the code you had as a way to prove I'm wrong, it won't work. At them moment it is working correctly with the solution I had posted through numerous testing.Oh. Not "the old version" then. Keep this thread in mind when you get weird behavior from a switch. And I do mean "when", not "if". Setting it to TRUE does not work and setting whole numbers does not work.I don't know what code you tried for those two attempts but the switch(TRUE) is in the correct form. Note "correct form". The reason it doesn't work is because of the conditions. Look at them: case $LRH_calcDepart >=0 && $LRH_calcDepart <=-2:Please give me an example value of $LRH_calcDepart that will satisfy that condition.You'll get light purple for every value >= -10 because that last case there is the only one that could ever possibly match. Why are you arguing and resisting what these senior level members are telling you?Texan knows something that has never been wrong in his/her experience. We're saying that it is, in fact, wrong. It's good to be a bit stubborn.Of course I'm still hoping for that "ah ha" moment where everything we're saying clicks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 10, 2014 Share Posted December 10, 2014 (edited) As for the logic $magnitude >=0 && $magnitude <=0.9 And the second is $magnitude >=1 && $magnitude <=1.9 If you make it a whole number then it will not know which color to use therefore it will be NULL. That is why the ranges are the way they are. This has been tested. less than and more than doesn't exempt the number. So I.E. if the number is 1.9 or less or great than 1 it will include that entire range. It doesn't exclude 1 or 1.9 from than range. If you have two switches using the same range then it would be NULL because it wouldn't know which one to inherit. Again, this as been tested. At them moment it is working correctly with the solution I had posted through numerous testing. Well, I will admit that the code example I posted was off, but if you actually read what I posted you would see that your case statements do, in fact, have gaps. And, your numerous tests are not valid if all you ever do is pass values that you know will pass. As, I asked before, what would be the result for a value such as 0.95? switch(TRUE) { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=1.9: $magScale = 'rgb(210, 238, 197)'; break; . . . The answer is there wouldn't be a result because it wouldn't match any of the conditions. My reasoning in the prior post was 100% correct. I just didn't modify the code correctly. It should be like this { case $magnitude >=0 && $magnitude <1: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <2: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <3: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <4: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <5: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; Note that there are no equal signs in the right conditions. That means there are zero gaps between the statements. A value could never intentionally or accidentally fall between them. As for your understanding of a switch, there is still a significant principle that is being overlooked. Here is what is in the manual switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; } This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found. Let me provide a detailed explanation of why your current logic "works" but why it works by accident. $magnitude = 1.5; //Test value $magScale = ''; switch($magnitude) { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=1.9: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <=2.9: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <=3.9: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <=4.9: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; } OK, let's walk through this step-by-step. 1. We set the variable $magnitude to 1.5 for testing purposes 2. $magScale is set to a default of an empty string 3. Here is where the real work start. The switch statement evaluates the contents in the parenthesis. This can really be anything, but is usually just a single value as you have it here. In this case, the value is 1.5. 4. The switch statement then compares the value evaluated previously (1.5) and COMPARES it (loosely) to the case statement. So, PHP checks to see if 1.5 is compares (is equal) to what is in the case statement $magnitude >=0 && $magnitude <=0.9 Let's run through that comparison step-by-step 1.5 == ($magnitude >=0 && $magnitude <=0.9) Replace value for $magnitude 1.5 == (1.5 >=0 && 1.5 <=0.9) Convert the inner comparisons to Booleans 1.5 == (TRUE && FALSE) Apply the AND condition 1.5 == (FALSE) Hmm, 1.5 Does not compare (loosely) to FALSE. The first switch fails. 5. Now here is where it get really interesting and illustrates why this works, for the wrong reason. Let's run through the second case the same way 1.5 == ($magnitude >=1 && $magnitude <=1.9) Replace value for $magnitude 1.5 == (1.5 >=1 && 1.5 <=1.9) Convert the inner comparisons to Booleans 1.5 == (TRUE && TRUE) Apply the AND condition 1.5 == (TRUE) This condition passws. But, NOT directly because the condition resulted in true. The reason is that the condition evaluated to true and when compared with any NON FALSE value would result in a TRUE condition because of how PHP does loose comparisons by default. Take a look at table 2 on this page. In fact, if you tested with a $magnitude of 0, the code would fail because you would end up with this comparison for the first case statement. 0 == TRUE Note that the manual states that the switch is comparing the value in the parens to the value in the case statements. It doesn't state that it is checking if the values in the case statements evaluate to TRUE - which is what you were thinking was happening. Edited December 10, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
Texan78 Posted December 10, 2014 Author Share Posted December 10, 2014 The 0-0.9 is mainly there for visual reference on the legend as if it was 0 nothing would show anyways as their is no such data that would be available for 0. Also there is a minimum threshold variable in the script that can be set to not show quakes below a certain richter scale. I.E. $setMinMagnitude = '0.1'; // minimum Richter Magnitude to display because keep in mind 0 will show nothing so it doesn't matter if it shows correctly for as there is no such data that will be returned for 0. I didn't mention this because it was irrelevant and I was trying to keep the question short and sweet. All I needed was a simple way to set the back ground color depending on the magnitude in a certain range. As I have never used a switch with ranges before as I stated in my initial post. What I came up with accomplishes this as it was what was suggested 8 months ago from this board when I first started this script and just got back around to working on it. As you can see the logic flows exactly as I wanted it to with the legend. As you can see 1-1.9 is green, 2-2.9 is yellow and so on. If you were to set each case statement with duplicate whole numbers it would not display correctly. It would show the first instance it came across. Therefore 2 would be green not yellow, which is what it goes from 1-1.9 then 2.9. See example from link. I tested it the other way, it didn't work. I am not trying to argue and I see your point but, it clearly doesn't work. http://www.mesquiteweather.net/wxquake.php It is hard to argue the logic when it is working and it is a very simple script. Could something break? Sure, if my host updates the PHP but even code that is correctly written could have to be changed as I have experienced in the past with numerous PHP5 updates. Now in the spirt of trying other alternatives I have tried your suggestion in another script. It is simply not working based of the examples I had to work with. $LRH_departScale = ''; if ($LRH_calcDepart >= 0 && $LRH_calcDepart < -2) { $LRH_departScale = 'rgb(210, 238, 197)'; } else if ($LRH_calcDepart >= -2 && $LRH_calcDepart < -4) { $LRH_departScale = 'rgb(210, 238, 197)'; } else if ($LRH_calcDepart >= -4 && $LRH_calcDepart < -6) { $LRH_departScale = 'rgb(210, 238, 197)'; } else if ($LRH_calcDepart >= -6 && $LRH_calcDepart < - { $LRH_departScale = 'rgb(210, 238, 197)'; } else if ($LRH_calcDepart >= -8 && $LRH_calcDepart < -10) { $LRH_departScale = 'rgb(210, 238, 197)'; } else if ($LRH_calcDepart >= -10) { $LRH_departScale = 'rgb(210, 238, 197)'; } Now when I remove the $LRH_departScale = ''; I get a undefined error naturally. Just for clarification, $LRH_calcDepart will be static even though it is dynamic content. So for example. if (12 >= 0 && 12 < -2) { $LRH_departScale = 'rgb(210, 238, 197)'; } else if (12 >= -2 && 12 < -4) { $LRH_departScale = 'rgb(210, 238, 197)'; So yes I understand by logic, all those numbers are less than 12. So it could set the $LRH_deparScale to any of those colors. But, since it is checking to see if 12 is within that set range and if it isn't then move on to the one until it is within a range then set that variable to that color for that range. Which I understand where the TRUE statement comes in. If var X is TRUE I.E. within that range then set Y. I am not arguing it is wrong, I am saying it doesn't work. Quote Link to comment Share on other sites More sharing options...
Texan78 Posted December 10, 2014 Author Share Posted December 10, 2014 Well, I will admit that the code example I posted was off, but if you actually read what I posted you would see that your case statements do, in fact, have gaps. And, your numerous tests are not valid if all you ever do is pass values that you know will pass. As, I asked before, what would be the result for a value such as 0.95? I can see your reasoning in that case but, the data would never be presented in that manner. It will also be 0.1, 0.2, 0.3, 1.1, 1.2, 1.3, etc.... Which is why I set the ranges the way I did to take the dec point into consideration. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 10, 2014 Share Posted December 10, 2014 Texan78, 1) Just because values of 0.95 are not valid today, does not mean they would not be valid in the future. Why write code with obvious gaps when you can write it one time that will not break in the future if the input values were to be more precise as some later point. It is simply bad practice. 2) Yes, your code "works", but it is working by accident. I tried giving you a very detailed explanation of why that is so. Just as above, why write code that is flawed. If a future release of PHP was, for example, to change the comparison between the evaluated value and the case value to be strictly compared the code you have would fall apart completely. It is not working because the conditions in your case statements are being evaluated to TRUE. It is working because those conditions are evaluated to TRUE and then they are being compared to $magnitude from the switch constructor. In fact, with what you have, you could just as easily put any trivial value into the switch statement other than a value not interpreted as false (such as FALSE or 0). This would work exactly the same for the values you tested for $magnitude $magScale = ''; switch('ABCDEFGHIJKLMNOPQRSTUVWXYZ') { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=1.9: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <=2.9: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <=3.9: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <=4.9: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; } Does that make sense or seem right to you? Quote Link to comment Share on other sites More sharing options...
Texan78 Posted December 10, 2014 Author Share Posted December 10, 2014 Just because values of 0.95 are not valid today, does not mean they would not be valid in the future. Seeing that form of measurement which is the Richter scale has been used since 1935. I don't see it ever changing as it is the formulated method used worldwide. To change that would be like changing the way you add or substract. So it won't change. The data will always be consistent. So it is irrelevant and the ranges work. You remove Switch ('ANYTHING') then it doesn't work at all. So something has to be there. Doesn't matter what you put but, it has to be something. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.