1

this is my script which works perfectly on php 8.0, but on php 7.4 its malfunctioning.

$fileName = 'Usecase.csv'; 
$tempName = 'temp.csv';     
$inFile = fopen($fileName, 'r');
$outFile =  fopen($tempName,'w');
while (($line = fgetcsv($inFile)) !== FALSE) 
{
    if(($line[0] == "$fin") )  
    {
        $line = explode (",", "$tempstr10"); 
        $asd=$asd+1;  
    } 
    fputcsv($outFile, $line );
}
fclose($inFile);
fclose($outFile);
unlink($fileName);
rename($tempName, $fileName);
    if( $asd==0  && filesize("Usecase.csv")>0) 
    { file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); } 
 
    if( $asd==0  && filesize("Usecase.csv")==0)
    { file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); }

I tried printing the error if any , but nothing was printed.

1: So whats the best practice to track an issue like this ? 2: according to my limited knowledge this script uses simple functions which are supported by both php7 and php8 so why this issue ?

Thank you

Update: this part of the code reads a csv file and if the csv file contains a certain string then it will replace the entire row that the string was found in.

$fileName = 'Usecase.csv'; 
$tempName = 'temp.csv';     
$inFile = fopen($fileName, 'r');
$outFile =  fopen($tempName,'w');
while (($line = fgetcsv($inFile)) !== FALSE) 
{
    if(($line[0] == "$fin") )  
    {
        $line = explode (",", "$tempstr10"); 
        $asd=$asd+1;  
    } 
    fputcsv($outFile, $line );
}
fclose($inFile);
fclose($outFile);
unlink($fileName);
rename($tempName, $fileName);

technically $line[0] == "$fin" checks if the value of $fin matches the $line[0], if yes the replace the entire $line with $tempstr10 if string is matched then it will increment the value of the $asd and the next part of the code wont be executed

otherwise this will be executed based on the if statement :

    if( $asd==0  && filesize("Usecase.csv")>0) 
    { file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); } 
 
    if( $asd==0  && filesize("Usecase.csv")==0)
    { file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); }

Issue : In php8 when a string already existed in csv file it would replace that entire row (the row that contained the string ) with the new string but in php7 it doesnt replace it just adds the new line with the new value

Sam
  • 161
  • 1
  • 11
  • 2
    What is the definition of "malfunctioning"? To what extent does the execution result differ between PHP7 and PHP8? Maybe settings from php.ini (which is usually version dependent, e.g. resides in the particular folder of your php installation) are causing the trouble. – mynd Jul 10 '21 at 10:13
  • What exactly did you do to try and show the error? And what does "malfunctioning" mean specifically? What did you expect the code to do, and what exactly did it do instead? – ADyson Jul 10 '21 at 10:14
  • @mynd i have added the update in the OP to explain the "malfunctioning" – Sam Jul 10 '21 at 10:27
  • @ADyson I tried to print_r the last error at multiple points of the code but nothing showed up , and i have also updated the OP to explain the "malfunctioning" – Sam Jul 10 '21 at 10:29
  • See https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php for an easier and more comprehensive way to get error messages to show up – ADyson Jul 10 '21 at 11:17
  • 1
    The answer to "what's the best practice to track an issue like this?" is **always break the problem down**. Although only a few lines long, this code contains a lot of different things: reading data from a file, writing data to a file, renaming and deleting files, CSV parsing, string manipulation, comparisons, loops, conditions... You need to examine and simplify so that you can see which of those things is actually behaving differently. Replace parts which run the same in both versions with hard-coded values; delete parts _after_ the problem shows; keep narrowing it down. – IMSoP Jul 10 '21 at 14:56

1 Answers1

2

Not sure if this is the root of a problem, but should be payed attention to: in php since version 8 there are different rules of number with string comparison. See https://www.php.net/releases/8.0/en.php#saner-string-to-number-comparisons.

Consider using "strict" equality check (=== instead of ==), to avoid implicit type conversions, and convert types explicitly before comparing.

E.g.

if((string) $line[0] === (string) $fin)
Ilia Yatsenko
  • 779
  • 4
  • 7