John Davidson

php - If Then Comparison not working? Different Text Type?

0 comments
Message:


I'm completely lost right now on a simple if then check for php.


My current code is:


 while ($row = fgetcsv($handle, 100000, ",")) {
if ($header === null) {
$header = $row;

$identified = false;
echo "Columns found in CSV<br/>";

foreach ($row as $value) {

echo 'Values: [' . strtolower($value) . ":" . 'first name]<br/>';
if (strtolower($value) == strval('first name') {
echo "is equal";
} else {
echo "not equal";
}
}
}

Output is currently


Columns found in CSV
Values: [first name:first name]
not equal

What am I missing??? This should be an extremely simple thing to do. I will note that the $value is being set to text being read from a CSV file. Is it possible the actual text is being messed with some type of encoding issue? I've also tried to do like


$value = utf8_encode($value);

But this isn't fixing the issue either. I'm completely stumped on this issue. I'm trying to find out why the IF statement isn't working correctly and the values isn't equal to return "is equal" as the response.



My Answer:
"r")) {
if ($row[0] == "apple") {
echo "Found apple!";
} else {
echo "Not apple";
}
}

This code should work as expected, but if it's not working for you, there could be a few reasons why. One possibility is that the text in the CSV file is not exactly "apple" (e.g. there could be extra spaces or special characters). To troubleshoot this, you can try using the trim() function to remove any leading or trailing whitespace from the text before comparing it:

if (trim($row[0]) == "apple") {
echo "Found apple!";
} else {
echo "Not apple";
}

Another possibility is that the text in the CSV file is in a different text encoding than what PHP is expecting. You can try using the mb_convert_encoding() function to convert the text to the correct encoding before comparing it:

if (mb_convert_encoding($row[0], "UTF-8") == "apple") {
echo "Found apple!";
} else {
echo "Not apple";
}

If none of these solutions work, you may need to further investigate the data in the CSV file and how it is being read by fgetcsv().

Rate this post

3 of 5 based on 6885 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us