Files and Streams
CSV Fields
CSV text can be read from a file and parsed into selected scalar fields.
CSV Fields
csv_fields.php
<?php
$qty = ;
$file = "php_csv_file.txt";
file_put_contents($file, "item,qty\npen," . $qty . "\n");
$row = explode("\n", trim(file_get_contents($file)))[1];
$item = str_getcsv($row)[0];
$count = (int) str_getcsv($row)[1];
unlink($file);
echo "item=" . $item . "\n";
echo "count=" . $count . "\n";
<?php
$qty = ;
$file = "php_csv_file.txt";
file_put_contents($file, "item,qty\npen," . $qty . "\n");
$row = explode("\n", trim(file_get_contents($file)))[1];
$item = str_getcsv($row)[0];
$count = (int) str_getcsv($row)[1];
unlink($file);
echo "item=" . $item . "\n";
echo "count=" . $count . "\n";
<?php
$qty = ;
$file = "php_csv_file.txt";
file_put_contents($file, "item,qty\npen," . $qty . "\n");
$row = explode("\n", trim(file_get_contents($file)))[1];
$item = str_getcsv($row)[0];
$count = (int) str_getcsv($row)[1];
unlink($file);
echo "item=" . $item . "\n";
echo "count=" . $count . "\n";
parsed fields
Parse only the fields needed for the lesson so public replay stays scalar.