The following code tests to see how certain values come out against PHP functions:
//array to hold test values
$values = array
(
'empty string' => '',
'space' => ' ',
'false' => false,
'true' => true,
'empty array' => array(),
'null' => null,
'0 as string' => '0',
'0 as integer' => 0,
'0 as float' => 0.0,
'$var declared but without a value' => $var
);
foreach($values as $key => $value):
$result['isset'][$key] = isset($value);
$result['empty'][$key] = empty($value);
$result['is_null'][$key] = is_null($value);
$result['is_numeric'][$key] = is_numeric($value); //added this line on 10/7/13
$result['strlen'][$key] = strlen($value); //added this line on 12/23/13
endforeach;
//view results by function
echo "<h3>view results by function</h3><pre>" . print_r($result,1) . "</pre>";
The array outputted from this code is as follows:
Array
(
[isset] => Array
(
[empty string] => 1
[space] => 1
[false] => 1
[true] => 1
[empty array] => 1
[null] =>
[0 as string] => 1
[0 as integer] => 1
[0 as float] => 1
[$var declared but without a value] =>
)
[empty] => Array
(
[empty string] => 1
[space] =>
[false] => 1
[true] =>
[empty array] => 1
[null] => 1
[0 as string] => 1
[0 as integer] => 1
[0 as float] => 1
[$var declared but without a value] => 1
)
[is_null] => Array
(
[empty string] =>
[space] =>
[false] =>
[true] =>
[empty array] =>
[null] => 1
[0 as string] =>
[0 as integer] =>
[0 as float] =>
[$var declared but without a value] => 1
)
[is_numeric] => Array
(
[empty string] =>
[space] =>
[false] =>
[true] =>
[empty array] =>
[null] =>
[0 as string] => 1
[0 as integer] => 1
[0 as float] => 1
[$var declared but without a value] =>
)
[strlen] => Array
(
[empty string] => 0
[space] => 1
[false] => 0
[true] => 1
[empty array] =>
[null] => 0
[0 as string] => 1
[0 as integer] => 1
[0 as float] => 1
[$var declared but without a value] => 0
)
)
The results would be much more friendly to view in an HTML table. So far I have only had success building the row that contains th tags:
$html = "\n<table>\n<tr>\n\t<th>test</th>\n";
//table header row
foreach ($result as $key => $value):
$html .= "\t<th>$key</th>\n";
endforeach;
$html .= "</tr>\n";
//add detail rows here
$html .= "</table>\n";
I am having much difficulty figuring out how to build the detail rows properly. The goal is to display one test name per row (). The tests are the keys in the $values array. The s would be the test name, answer to isset, answer to empty, answer to is_null, answer to is_numeric, answer to strlen. How can this be achieved using foreach loops?