Jump to content

Variables and switch() function


Schlo_50

Recommended Posts

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

Link to comment
Share on other sites

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.';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.