How can you count the number of data records with a value of '1' in PHP?
To count the number of data records with a value of '1' in PHP, you can iterate through the data records and increment a counter every time a record with a value of '1' is encountered. You can achieve this by using a loop to go through each record and checking if the value is '1', then incrementing a counter variable if it is. Finally, you can output the total count of records with a value of '1'.
$data = [0, 1, 1, 0, 1, 0, 1]; // Sample data records
$count = 0;
foreach ($data as $record) {
if ($record == 1) {
$count++;
}
}
echo "Number of data records with value '1': " . $count;