What is the purpose of using fgetcsv() in PHP?
fgetcsv() is used in PHP to read and parse a line from a CSV file, returning an array of values. This function is helpful when dealing with CSV files as it handles the parsing of each line and separates the values into an array, making it easier to work with the data. It is commonly used when needing to import or process CSV files in PHP.
$row = 1;
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Keywords
Related Questions
- What are some alternative methods to $_GET[] for accessing parameters in PHP scripts embedded in web pages?
- How can I generate an activation link when inserting an article into a MySQL database using PHP?
- What are the potential issues with integrating PHP and CSS for setting background colors dynamically?