Schlo_50 Posted May 8, 2008 Share Posted May 8, 2008 Hi there, Couple of questions. Is is possible to define my variable $low_boundary to be any number from 0 - 20000? Or is there a better way to do this? Im testing out the switch() function for fun and am setting my search variable to be 13999 for example. I want to have three cases in total acting as boundaries. ($low_boundary = 0 - 20000, $med_boundary = 21000 - 40000 and $high_boundary = 41000 - 100000) I have this so far, but don't know how to make my cases become parameters as it were. <?php $wage = "13999"; $low_boundary = ""; $med_boundary = ""; $high_boundary = ""; print "Desired Wage: £$wage<br />"; switch ($wage){ case "$low_boundary": print "Low Band: £0 - £20000"; break; case "$med_boundary": print "Medium Band: £21000 - £40000"; break; case "$high_boundary": print "High Band: £41000 - £100000"; break; default: print "Your desired wage is too high."; break; } ?> Can somebody help me out? Thanks, I'd really like to learn the boundaries thing and get the switch funtion working nicely. I think switch could be very useful in the future! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/104685-variables-and-switch-function/ Share on other sites More sharing options...
clearstatcache Posted May 8, 2008 Share Posted May 8, 2008 i think if else is more applicable in this situation.... Quote Link to comment https://forums.phpfreaks.com/topic/104685-variables-and-switch-function/#findComment-535774 Share on other sites More sharing options...
thebadbad Posted May 8, 2008 Share Posted May 8, 2008 You could use a switch on true: <?php //define boundaries $low_boundary = '0-19999'; $med_boundary = '20000-39999'; $high_boundary = '40000-100000'; //explode at dashes to get an array with lowest and highest values for each level $low = explode('-', $low_boundary); $med = explode('-', $med_boundary); $high = explode('-', $high_boundary); //set sample wage $wage = '13999'; //do the switch! switch(true) { case $wage >= $low[0] && $wage <= $low[1]: echo 'Low Band: £0 - £19999.'; break; case $wage >= $med[0] && $wage <= $med[1]: echo 'Medium Band: £20000 - £39999.'; break; case $wage >= $high[0] && $wage <= $high[1]: echo 'High Band: £40000 - £100000.'; break; default: echo 'Out of bounds.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/104685-variables-and-switch-function/#findComment-535777 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.