r/PHPhelp • u/Punkygdog • Dec 11 '24
Solved stuck with a IF problem
Solved! I am working on a php, i have section that is as follows..
if(strpos($M['codes'], 'OVC' ) == true ) {
$output .= "Color: 255 255 255\n Text: -17, -13, 1, ".$M['codes']."\n";
}
$mcodes does equal on OVC however, it outputs a blank line anytime the data being parsed, if i set it to !== true, then it outputs $mcodes on every line entry
I am trying to get it to ONLY post the codes line if it equals OVC, or really anything than SKC.
Any hints, or tricks
0
Upvotes
2
u/MaxxB1ade Dec 11 '24
if(strpos($M['codes'], 'OVC' ) !== false ) {$output .= "Color: 255 255 255\n Text: -17, -13, 1, ".$M['codes']."\n";}2
u/colshrapnel Dec 11 '24 edited Dec 11 '24
A condition like this
strpos( ) == trueshould never be used. You don't compare values of different type. Please read the strpos' manual page in order to learn the following things:2
u/MateusAzevedo Dec 11 '24
Then use
if ($M['codes'] === 'OVC');Then
if ($M['codes'] !== 'SKC')strposreturn the index (int) where the substring was found. In case$M['codes']start with or isOVC, then the function returns0. In a loose comparison,0evaluates tofalseand that's why comparing it withtruedoesn't work and why the documentation has a big read warning.1
u/eurosat7 Dec 11 '24
You mean something like the official documentation for that method? php.net/strpos
1
u/Aggressive_Ad_5454 Dec 11 '24
Specifically.
strpos( ‘OVC’, ‘OVC’ )returns the integer zero. That function, according to the Return Values section of its doc either returns an integer or the valuefalse. So your code should say!== falsewhere it says== true.More generally, the
==and!=operators automatically typecast, so the expression0 == trueis always false. Zero is falsy and all other numbers are truthy.===and!==do not typecast, so0 === falseis false.0
u/colshrapnel Dec 11 '24
Can be reduced to "anything than SKC". If that's your actual goal, then you need a completely different expression.