if(@$_POST['csv'] == "1"){ }else{ ?> php } if(@$_POST['csv'] == "1"){ header('Content-Type: application/excel'); header('Content-Disposition: attachment; filename="sample.csv"'); $user_CSV[0] = array('first_name', 'last_name', 'age'); // very simple to increment with i++ if looping through a database result $user_CSV[1] = array('Quentin', 'Del Viento', 34); $user_CSV[2] = array('Antoine', 'Del Torro', 55); $user_CSV[3] = array('Arthur', 'Vincente', 15); $fp = fopen('php://output', 'w'); foreach ($user_CSV as $line) { // though CSV stands for "comma separated value" // in many countries (including France) separator is ";" fputcsv($fp, $line, ','); } fclose($fp); }else{ }