alnahian2003's avatar

Switch statement is not working perfectly!

Hey folks, this is a vanilla PHP-related question. A friend of mine is writing this switch statement to perform a basic logical operation and then output the result based on a working logic. But it seems like he ain't getting any accurate results. I tried to solve this simple problem but it also drove me crazy just like my friend.

[NOTE: WE ARE NOT DOING ANY FINANCIAL CALCULATIONS AND THIS CODE WON'T GO INTO PRODUCTION. IT's JUST BASIC PHP STUFF]

<?php
$val = 0;
switch ($val) {
	case ($val >= 1 && $val !=
		0):
		echo "I have {$val} dollar only";
		break;

	case ($val < 1):
		echo "I'm in debt :(";
		break;

	default:
		echo "I'm totally broke :/";
}
?>

What he trying to do is:

He is checking the value of $val in the first case if it's above 1 and is not equal to 0. If the logic passes then echo out something and break. And I hope you've figured out the rest.

But the problem is, whenever I run the code after setting the value of $val equals to 0, I get this I have $0 only ... seems like the switch statement is not working as expected.

What have I done wrong here?

0 likes
7 replies
thinkverse's avatar

Your default case will never be reached, given the case above it, $val < 1 will match for anything below 1, and last time I checked, 0 is less than one.

var_dump(0 < 1); // 0 less than 1 is true

Switches aren't really great at handling complex comparisons, it's best to stick to if-statements for that.

$val = 0;

if ($val > 0) {
    echo "I have {$val} dollar only";
} else if ($val == 0) {
    echo "I'm totally broke :/";
} else {
    echo "I'm in debt :(";
}
1 like
alnahian2003's avatar

@thinkverse that's true and also what I was thinking. But yes, now I get the idea after reading the official manual of the switch statement from PHP.net. As we are performing any logical operation based on the value of $val in case, I think I should pass the boolean true inside the switch at first just to make it run. Now my code works as expected:

$val = -1;
switch (true) {
	case ($val >= 1 && $val !=
		0):
		echo "I have $$val only";
		break;

	case ($val < 0):
		echo "I'm in debt :(";
		break;

	default:
		echo "I'm totally broke :/";
}

// while $val = -1; return I'm in debt
// while $val = 0; returns the default value
// while $val = 100; returns as expected

Sinnbeck's avatar

You want them all to check against true

switch (true) { 
1 like
click's avatar

You can't use a switch like this; try

switch(true) {
	case ($val >= 1 && $val !=
		0):
		echo "I have {$val} dollar only";
		break;

	case ($val < 1):
		echo "I'm in debt :(";
		break;

	default:
		echo "I'm totally broke :/";
}

or just if/else or even better if you have php 8, use match() https://www.php.net/manual/en/control-structures.match.php#example-132

match(true) {
   $val > 0 => "I have {$val} dollar only",
   $val < 0 => "I'm in debt :(", 
   default => "I'm totally broke :/",
}
2 likes
alnahian2003's avatar

@click You're absolutely right! This is what I got after replacing the switch with a match exp

$val = 0;

echo match (true) {
	$val >= 1 => "I have ৳{$val} only",
	$val < 0 => "I'm in debt :(",
	default => "I'm totally broke :/"
};

and it works totally fine! Thank you everyone

alnahian2003's avatar
alnahian2003
OP
Best Answer
Level 3

The problem is now solved. I replaced the switch with a match expression. Even with a switch statement, I had to pass true rather than $val inside the switch(/here/)

$val = 0;

echo match (true) {
	$val >= 1 => "I have ৳{$val} only",
	$val < 0 => "I'm in debt :(",
	default => "I'm totally broke :/"
};

// using switch statement
switch (true) {
	case ($val >= 1 && $val !=
		0):
		echo "I have $$val only";
		break;

	case ($val < 0):
		echo "I'm in debt :(";
		break;

	default:
		echo "I'm totally broke :/";
}

Thank you, everyone!

1 like

Please or to participate in this conversation.