Or . . . empty will work:
foreach ($_POST as $name=>$data) {
if (! empty($data)) { $msg .= "$name: $data<br>";}
}
One of the advantages to working with double quotes is allowing the scalar variables ($name, $data) to interpolate.
What does the => mean?
You know, I don't know what they call that operator, it's also used in Perl. :-) It's a symbol used to dereference an associative array, not to be confused with >= (greater than or equal.) The array key is left, value right. It's also used to set associative array values. The following two are equivalent.
$myarray = array (
'foo' => 'example.com',
'bar' => 'webmasterworld.com'
);
$myarray = array ();
$myarray['foo'] = 'example.com';
$myarray['bar'] = 'webmasterworld.com';
Either is dereferenced like so. Again, the following are equivalent but the second example is silly if you already have $value in hand -= but there are cases where you'd use it.
foreach ($myarray as $key => $value) {
echo "<p>$key $value</p>";
}
foreach ($myarray as $key => $value) {
echo "<p>$key " . $myarray[$key] . "</p>";
}
(or use {} to avoid concatenation) While we're at it, an associative array with a
list array as members.
$myarray = array (
'foo' => array('apples','oranges','pears'),
'bar' => array('lions','tigers','bears')
);
Since there's no
key, list arrays are dereferenced as single members. These two are also equivalent, but the first is more efficient - the array indices are not needed to reference the members, although there are cases where accesing it by index is needed.
foreach ($myarray as $key => $value) {
echo "<p><strong>Key:</strong> $key <strong> Values:</strong> ";
foreach ($value as $v) {
echo " $v ";
}
echo "</p>";
}
foreach ($myarray as $key => $value) {
echo "<p><strong>Key:</strong> $key ";
echo " <strong>Values:</strong> " . $value[0] . " " . $value[1] . " " . $value[2] . "</p>";
}
OR ... more excessive, but as mentioned, sometimes access by index is needed.
foreach ($myarray as $key => $value) {
$len = count($value)-1; // starts at zero
echo "<p><strong>Key:</strong> $key ";
for ($j=0;$j<=$len;$j++) {
echo " " . $value[$j] . " ";
}
echo "</p>";
}