If and statement

Hi there I am trying to use a if and statement combination.

i have a kitchen unit size i am trying to achieve this result

if the unit is greater than 601 then 2 handles else 1 handle
also
if the unit is greater than 601 and drawline then 4 handles else 2 handles

the code i have below outputs 1 handle / 2 handle

if ($base1width > 601) {
    echo" 2 handles / ";
} else {
    echo" 1 handle / ";
}

if ($base1width > 601 && $drawline) {
    echo" 4 handles / ";
} else {
    echo" 2 handles / ";
}

i have tried using the else if but get error

any help please

if a user inputs a value less than 601 the should read 1 handle, if inputs more than 601 should read 2 handles

but

if a user inputs a value less than 601 and selects drawline the should read 2 handles, if inputs more than 601 and drawline should read 4 handles.

Your statement of what to do doesn’t cover the case where the width is exactly 601. Your example code implies that less-than or equal should be used in this case.

In general, you should avoid writing out conditional logic with hard-coded values in it to handle every possible choice, and instead use a data driven design, where the choices are defined in a data structure (an array or a database table), that general purpose logic or an sql query operates on to find the result.

See the following array based solution -

// inputs - $base1width, $drawline
$base1width = 602;
$drawline = true;

// define a data structure that maps the input choices to a result
$map = [];
$map[] = ['width'=>601,'width_comparison'=>'<=','drawline'=>false,'num_handles'=>1];
$map[] = ['width'=>601,'width_comparison'=>'>','drawline'=>false,'num_handles'=>2];
$map[] = ['width'=>601,'width_comparison'=>'<=','drawline'=>true,'num_handles'=>2];
$map[] = ['width'=>601,'width_comparison'=>'>','drawline'=>true,'num_handles'=>4];


// the following code loops until it finds the first true comparison
$found = false;
foreach($map as $element)
{
	switch($element['width_comparison'])
	{
		case '<=':
			if($base1width <= $element['width'] && $element['drawline'] == $drawline)
			{
				$found = true;
				break 2; // exit from the foreach loop
			}
		break;
		case '>':
			if($base1width > $element['width'] && $element['drawline'] == $drawline)
			{
				$found = true;
				break 2; // exit from the foreach loop
			}
		break;
	}
}

// test the result
if($found)
{
	echo 'Result is - '.$element['num_handles'].' handles.';
} else {
	echo 'No result found.';
}

You can modify or add choices to this just by changing or adding entries in the $map array. The only time you would edit the program logic is if you add more things that go into deciding what result to match. The switch/case statement can be modified to handle the case where an equal match is a different result.

Thank you i tried this but each result was the same ?

Since we don’t know what result you got, I’m not sure what further help you are expecting.

I dont know what your business rules exactly are, but wouldn’t it be more efficient to say, 1 handle per x inches?

if ($base1width > 601) {
    echo" 2 handles / ";
} else {
    echo" 1 handle / ";
}

if ($base1width > 601 && $drawline) {
    echo" 4 handles / ";
} else {
    echo" 2 handles / ";
}

This creates and either or, not a selection for the best or most; and we don’t know what the drawline is.

sorry guys if i not explained it properly.

basically base1width is a value a user inputs. Its the width of a kitchen unit, i also have a check box with the name drawline. The checkbox is unchecked.

Basically they input a width they want the unit to be. If they do not tick the drawline tick box it determines the unit has just one handle. If the user inputs a width and ticks that it is also a drawline then the unit has 2 handles. However if the unit is more than 601 wide then the unit will have 2 handles and if its also a drawline then it has 4 handles

if they input a width that is less than 601 then it should output 1 handle
if they input a width more than 601 then it should output 2 handles
if they input a width that is less than 601 and also tick the checkbox to say its drawline then output 2 handles
if they input a width that is more than 601 and also tick the checkbox to say its drawline then output 4 handles

You still haven’t answered the question on what happens when the width is 601

the width will never be 601 it could be 600 or next size will be 700

if the unit is 600 or less then it will have 1 handle if the unit is over 600 it will have 2 handles

this is worked on kitchen unit standard sizes which are
150
200
250
300
350
400
450
500
550
600
700
800
900
1000

Here you go. Test by changing $unitSize and $drawline from true or false.

<?php declare(strict_types=1);

$handles = 1;
$unitSize = 602;
$drawline = true;

$handles = $unitSize > 601 ? $handles += 1 : $handles;
$handles =  !empty($drawline) &&  $unitSize > 601 ? $handles += 2 : $handles;

echo $handles;
Sponsor our Newsletter | Privacy Policy | Terms of Service