This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP expresses two different strings to be the same
I have a problem understanding what's causes this weird behavior in a switch case instruction.
The code is this:
<?php
$myKey = "0E9";
switch ($myKey) {
case "0E2":
echo "The F Word";
break;
case "0E9":
echo "This is the G";
break;
default:
echo "Nothing here";
break;
}
?>
The result of this instruction should be This is the G
Well, not so. always returns The F Word
If we reverse the 0E9 left instructions for the beginning and try to find the value 0E2
<?php
$myKey = "0E2";
switch ($myKey) {
case "0E9":
echo "The G String";
break;
case "0E2":
echo "The F Word";
break;
default:
echo "Nothing here";
break;
}
?>
Now returns always This is the G
0E2 and 0E9 values are not interpreted as text? Those Values are reserved?
Someone can explain this behavior?
"0E2" == "0E9" is true because they are numerical strings.
Note: switch use loose comparision.
Check this question: PHP expresses two different strings to be the same.
Numeric strings such as these are equal to each other .. always. Unfortunately, there is no way to force an equivalence comparison via switch. You just have to use if:
if ($myKey === '0E9') {
echo 'g';
}
else if ($myKey === '0E2') {
echo 'f';
}
else {
echo "Nothing here";
}
You could also trim the leading zero, I suppose.
Related
I am trying to compare my string like below.
$recieved = "SR1";
if ($recieved == "SR1" || $recieved == "SR2"|| $recieved == "SR3"|| $recieved == "SR4"){
echo "matching";
}
else{
echo "not matching";
}
This is working fine but I want to ignore case sensitivity checking like below
sr1
Sr1
sR1
I don't have idea how can i achieve this?
So you could achieve that by using strtoupper function, also, I would suggest to use in_array like so:
$recieved = "SR1";
if (in_array(strtoupper($recieved), ['SR1', 'SR2', 'SR3', 'SR4'])) {
echo "matching";
} else {
echo "not matching";
}
I hope that works well for you.
We can try using preg_match in case insensitive mode:
$received = "sR1";
if (preg_match("/sr1/i", $received, $matches)) {
echo "match";
}
This approach would also be a good starting point if you might have the need to match sR1, SR1, etc., as it appears inside a larger string. In that case, we could try searching for \bsr1\b using preg_match in case insensitive mode.
Can someone please explain why the case "a" is never reached in below code and why it will always execute case 0
switch ("a") {
case 0:
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
PHP, like JavaScript or Perl, is a loosely-typed language and will attempt to guess what you want to do. In this case, it changed your string to the closest integer it could find, which is zero. In other words, "a" == 0 is a true statement in PHP.
More on this topic can be found in the PHP documentation. I suggest you typecast the value in the switch statement, or replace it with an if/elseif/else construct.
As of PHP 8.0, this behaviour has changed and now the integer value will always be changed to a string before comparison between the two types. Strictly typing and comparing your variables remains the recommended practice, however.
You can not used mix-cases in a switch statement as PHP will interpret the meaning of what you mean.
In layman's terms, it will try to find the 'value of "a"' which is not defined to the processor, and hence is 0 in this case.
Same will go for the code below:
<?php
$x = "a";
switch($x)
{
case "c":
echo "c";
break;
case 1:
echo "1";
break;
case 0:
echo "0";
break;
case "a":
echo "a";
break;
case false:
echo "false";
break;
default:
echo "def";
break;
}
?>
Documentation is available at PHP.net
The reason for this is because switch uses a loose comparison ==
That said:
if ("a" == 0) // TRUE
if ("a" == true) // TRUE
Pretty much anything else will evaluate to false. (except "a" == "a")
So, if you have the need to compare against both strings and integers, you should just convert to string for the comparison.
//$var = "a";
$var = 0;
$var = strval($var);
switch ($var) {
case '0':
echo "0";
break;
case 'a':
echo "a";
break;
}
The variable type used on case() should be same type used in switch().
<?php
switch ("a") {
case "0":
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
For integer type:
<?php
switch (1) {
case 0:
echo 0;
break;
case 1: // never reached because "a" is already matched with 0
echo 1;
break;
}
This question already has answers here:
How can I shorten this PHP if statement?
(2 answers)
Closed 9 years ago.
if($a === "b" || $a === "c" || $a === "d")
echo $a;
else
echo 'a is not........';
Is there a way to write this without repeating $a each time?
you're not checking anything. you're doing assignments, since you're using = and not ==.
There's very little you can do to optimize this code. It might be tedious to write out, but in execution terms it's not bad. Don't confuse "shorter" code with "more efficient". e.g
if (!in_array($a, array('a', 'b', 'c')) { ... }
is less code. But it will almost certainly perform WORSE than your code. Why? Because now you're making PHP create an array on-the-fly. It then has to, internally, loop over that array and compare each value against $a individually, exactly as your "tedious" if() is doing. So all you've done is reduce the amount of code written, but INCREASED execution time because you've added in the entire "create an array" requirement.
I’d be inclined to use a switch instead. I find it more readable.
switch ($a) {
case 'b':
case 'c':
case 'd':
echo $a;
break;
default:
echo 'a is not........';
break;
}
From the PHP manual: in_array()
if (in_array($a, array("b","c","d"))) {
echo $a;
} else{
echo 'a is not........';
}
This question already has answers here:
How to handle a PHP switch with different types?
(5 answers)
Closed 9 years ago.
The following switch case statement was supposed to execute the value of case "january": but instead it is executing the value of case 0: ,can anybody tell me why it is doing this?
CODE:
<?php
$a = "january";
switch ($a)
{
case 0:
case 3:
echo "The value is either 0 or 3";
break;
echo "0/2";
break;
case "january":
echo "january";
break;
case "march":
echo "The value is ";
break;
case 2:
echo "The value is 2";
break;
default:
echo "Here is ur default message";
break;
}
?>
By the way it is doing the same for the case "march"....?
$a = 'january';
var_dump($a == 0);
var_dump($a === 0);
var_dump($a == '0');
var_dump($a === '0');
Output:
bool(true)
bool(false)
bool(false)
bool(false)
Read up on Type Juggling. Apparently switch uses loose comparison:
Note:
Note that switch/case does loose comparision.
PHP has some problems that can cause this kind of behaviour, in this case the problem comes from the fact that you are using different types on your Switch.
Try this code:
echo (0 == "january") ? "true\n" : "false\n"; // Echoes true
One way to work around this is to use only strings on your switch, like so:
switch ($a)
{
case "0":
case "3":
echo "The value is either 0 or 3";
break;
echo "0/2";
break;
case "january":
echo "january";
break;
case "march":
echo "The value is ";
break;
case "2":
echo "The value is 2";
break;
default:
echo "Here is ur default message";
break;
}
This will work fine, because 0 == "0" but "january" != "0"
Switch uses simple == to compare the values of the variable with the ones on each case.
You need to put quotes around the numbers. You can't mix numbers and strings when using a switch statement.
<?php
$var = 0;
switch($var) {
case "a":
echo "I think var is a";
break;
case "b":
echo "I think var is b";
break;
case "c":
echo "I think var is c";
break;
default:
echo "I know var is $var";
break;
}
?>
Maybe someone else will find this fascinating and have an answer. If you run this, it outputs I think the var is a when clearly it's 0. Now, I'm most certain this has something to do with the fact that we're using strings in our switch statement but the variable we're checking is an integer. Does anyone know why PHP behaves this way? It's nothing too major, but it did give me a bit of a headache today.
Thanks folks!
If you compare an integer with a string, the string is converted to a number. So effectively your snippet is equivalent to:
$var = 0;
switch($var) {
case 0: // "a" gets converted to 0.
echo "I think var is a";
break;
case 0: // "b" gets converted to 0.
echo "I think var is b";
break;
case 0: // "c" gets converted to 0.
echo "I think var is c";
break;
default:
echo "I know var is $var";
break;
}
Which will produce I think var is a as output as the first case body gets executed. Even though there are 3 candidates, the first one is selected because it appears at the top.
In PHP, when you compare a string and an integer, whether it be in a switch statement or using the regular comparison operators, the string is converted to an integer (unless you're using the === operator).
And when converting a string to an integer, a string that doesn't start with either a digit or a sign is always converted to 0.
See the documentation here and here.