John Davidson

php - Why does the If condition never executes?

0 comments
Message:


I have written a simple script and a part of that script does not executes


This is the part that isnt woriking:


while (($line = fgetcsv($inFile)) !== FALSE) 

{
if(($line[0] === "$fin") )
{
echo ("$asd is 5");
$line = explode (",", "$tempstr10");
$asd=$asd+1;

}
fputcsv($outFile, $line );
}

More specifically the if statement's body isnt being executed.


this block of code is supposed to do this:
it checks if $line[0] === "$fin" ($fin has a value) meaning if value of $fin is found inside of $line[0] then execute this


    {
echo ("$asd is 5");
$line = explode (",", "$tempstr10");
$asd=$asd+1;

}

but it seems like even if the value of $fin is similar to the value inside of $line[0] its not executing the body of the if statement


What am i doing wrong here ?


UPDATE :


These are the values stored in the csv file :


"1234  "," 1234-12-31  " 
"12345 "," 1234-12-31 "
"123456 "," 1234-12-31 "

This is the result of var_dump :


string(4) "1234" string(4) "1234" string(4) "1234" string(4) "1234" string(4) "1234"


My Answer:
The issue may be with the comparison operator you are using in the if condition. In PHP, the strict comparison operator "===" checks both the value and the data type of the variables being compared. If the data type of $line[0] is not exactly the same as the data type of the value you are comparing it to, the condition will not be true.

You can try using the loose comparison operator "==" instead, which only checks the value of the variables being compared. This may allow the condition to execute as expected.

Here is the updated code snippet:

php
while (($line = fgetcsv($inFile)) !== FALSE) {
if ($line[0] == 'your_value_here') {
// Your code here
}
}


Replace 'your_value_here' with the value you are trying to compare $line[0] to. This should allow the if condition to execute if the values match, regardless of their data types.

Rate this post

4 of 5 based on 2358 votes

Comments




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