Tested in PHP 5.3.10 on CentOS.
In a script a run:
$test = "62 3/4";
if($pos = strpos($test,' ') !== false) {
$test= substr($test,0,$pos); // use $pos
}
// $test is "6"
And in another independent script I run:
if($pos = strpos($test,' ') !== false) {
$test = substr($test,0,strpos($test,' ')); // redo substr calculation
}
// $test is "62"
$pos should be 2 (the third character is a space, starting from zero, 0,1,2), so both $test should be “62”, no?
Operator precedence!
!==comes before=, so the test effectively becomeswhich is going to evaluate to either true or false, not the string position.
Always use explicit parens: