Why is the zero-indexed item in my $array not recognised?

<?php
$array = ["1", "3", "5", "7", "9"];

$compare = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

foreach ($compare as $c) {
echo '$array ';
if (array_search($c, $array, true)) {
echo 'does ';
} else {
echo 'does not ';
}
echo "contain $c <br>";
}
<?php
$array = ["1", "3", "5", "7", "9"];

$compare = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

foreach ($compare as $c) {
echo '$array ';
if (array_search($c, $array, true)) {
echo 'does ';
} else {
echo 'does not ';
}
echo "contain $c <br>";
}
results: $array does not contain 0 $array does not contain 1 $array does not contain 2 $array does contain 3 $array does not contain 4 $array does contain 5 $array does not contain 6 $array does contain 7 $array does not contain 8 $array does contain 9
18 Replies
ErickO
ErickO13mo ago
<?php
$array = ["1", "3", "5", "7", "9"];

$compare = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

foreach ($compare as $c) {
echo '$array ';
if (array_search($c, $array, true) !== false) {
echo 'does ';
} else {
echo 'does not ';
}
echo "contain $c <br>";
}
?>
<?php
$array = ["1", "3", "5", "7", "9"];

$compare = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

foreach ($compare as $c) {
echo '$array ';
if (array_search($c, $array, true) !== false) {
echo 'does ';
} else {
echo 'does not ';
}
echo "contain $c <br>";
}
?>
in general, you shouldn't rely on type inference in your if statements don't just if($value) actually compare it to something (unless it is already a boolean) if($value) !== 0 and to answer your question, when you rely on type inference php basically..."converts" the result to a boolean type, and 0 is a falsy value, the array_search function returns the index of the found item or false if it's not there which means when that function returns 0 (the index) it takes it as false
Blackwolf
Blackwolf13mo ago
is there an easy way to overcome this ?
ErickO
ErickO13mo ago
like I showed you just compare it to something if (array_search($c, $array, true) !== false)
Blackwolf
Blackwolf13mo ago
ahhhh gotcha, thank you so much
ErickO
ErickO13mo ago
otherwise what your if says is basically if(0) which is translated to if(false) funsies of type inference
Blackwolf
Blackwolf13mo ago
i had been using just one entry in my original array, and it being the zero-index was continually being false, and i thought it was other aspects of my code that was wrong - been pulling my hair out for 2 days now lol
ErickO
ErickO13mo ago
ah yes, when you have issues with if statements save the value to a variable instead of putting it inside the if statement, and log it so you can see what is actually being compared I am very confident in JS I still don't, but I'm more of a strict type programming language guy, so that's why it is good to learn how type inference works tho, that can cause a whole lot of bugs, so should give it a read
Blackwolf
Blackwolf13mo ago
i have made a note for future, thank you so much
ErickO
ErickO13mo ago
👍
Blackwolf
Blackwolf13mo ago
i am currently learning JS
ErickO
ErickO13mo ago
oh? well Js has the same issues so also a good read and I think they're very similar no, Hope? in that regard at lul
Blackwolf
Blackwolf13mo ago
in many of my arrays i put a fake entry in the zero index and start counting from 1
ErickO
ErickO13mo ago
oh PepeLaugh no need for that can you? like TS kind of types or python types where it is actually integrated just...extra optional surprisedpikachu
Blackwolf
Blackwolf13mo ago
what does that do? i don't use them either
ErickO
ErickO13mo ago
you just added one point to php on my list of languages to use for a project PepeLaugh
Blackwolf
Blackwolf13mo ago
PHP mostly, but learning some JS for dynamic user interaction
ErickO
ErickO13mo ago
eh probably not something you should worry about yet, altho it doesn't seem super restrictive but still, focus on type inference is good to know then you can get to strict types
Blackwolf
Blackwolf13mo ago
thank you again, I will sleep tonight now 🙂 when i say tonight, of course i mean toDAY lol another issue has arisen now lol i am actually comparing one array with 3 others, and it is "finding" a positive result where there is none, even in an empty array