So how do I test if a variable has a numeric value?
Jun 29, 2019
6
Level 2
How do you test if a variable is numeric or not?
I just finish creating a CSV upload function and am pretty happy with it, but feel it could be improved.. I would like to have it skip the header or basically any row where $data [5] is not numeric. I think a CONTINUE will do the trick, but I need to know how to test if a variable is numeric in value or not.
I used this code:
if (($handle = fopen ( request()->file('file') , 'r' )) !== FALSE) {
while ( ($data = fgetcsv ( $handle, 1000, ',' )) !== FALSE ) {
$reading = new Utility_Meter_Readings ();
$reading->property_id = $request->property_id;
$reading->utility_id = $request->utility_id;
$reading->resource_type = $request->resource_type;
// if($data [5] ??) {
// continue;
// }
$date = date_create($data [3]);
$reading->time = date_format($date, 'U');
$reading->kwh = $data [5];
$reading->save ();
}
fclose ( $handle );
}
I got it here: https://justlaravel.com/import-csv-data-store-database/
It seems to be more corePHP than Laravel. So how do I test if a variable has a numeric value?
Level 67
1 like
Please or to participate in this conversation.