User specification
- "
var_dump($GLOBALS)is ugly as...without wamp"
General specification
- Produce a PHP function to display the contents of
$GLOABLSin a user-friendly manner - Must produce properly indented HTML
- Must include appropriate CSS classes for customisability
So far, this is what I've got:
function __printRow($key, $value, $row)
{
$type = gettype($value);
$typeAndValue = __valueAndString($type, $value);
$typeLabel = $typeAndValue['type'];
$value = $typeAndValue['value'];
echo _tab(5) . "<tr class=\"_v_row_alt_" . $row % 2 . "\">\n" .
_tab(6) . "<td class=\"_v_key\">$key</td>\n" .
_tab(6) . "<td class=\"_v_type _v_typelabel_$type\">$typeLabel</td>\n" .
_tab(6) . "<td class=\"_v_value _v_type_$type\">$value</td>\n" .
_tab(5) . "</tr>\n";
}//End __printRow()
function _tab($num)
{
$tabs = "";
for($i = 0; $i < $num; $i++)
{
$tabs .= "\t";
}//End for
return $tabs;
}//End _tab()
function __valueAndString($type, $value)
{
$return = ['type' => $type, 'value' => $value];
if($value === null)
{
$return['type'] = $return['value'] = 'null';
return $return;
}//End if
switch($type)
{
case 'array':
$return['value'] = '[';
$count = count($value);
$return['type'] = "$type <font class=\"_v_length\">($count)</font>";
for($i = 0; $i < $count; $i++)
{
$subType = $value[$i] === null ? 'null' : gettype($value[$i]);
$subTypesAndValues = __valueAndString($subType, $value[$i]);
$subValue = $subTypesAndValues['value'];
$return['value'] .= "<font class=\"_v_type_$subType\">$subValue</font>";
if(($i + 1) < $count)
{
$return['value'] .= ', ';
}//End if
}//End for
$return['value'] .= ']';
break;
case 'boolean':
$return['value'] = $value ? 'true' : 'false';
break;
case 'string':
$return['type'] = "$type <span class=\"_v_length\">(" . strlen($value) . ')</span>';
$return['value'] = "'$value'";
break;
}//End switch
return $return;
}//End __valueAndString()
function ___v()
{
$globals = $GLOBALS;
unset($globals['GLOBALS']); //Remove reference to $GLOBALS
if(!isset($globals['_SERVER']))
{
//Didn't appear when run on a VM, not 100% sure that's why
$globals['_SERVER'] = $_SERVER;
}
ksort($globals);
echo "\n" . _tab(2) . "<div class=\"_v_container\">\n";
if(empty($_SESSION))
{
echo _tab(2) . "<div class=\"_v _v_info\"><span class=\"_v_info_var_name\">" .
"Session</span> is " . (!isset($_SESSION) ? 'not set' : 'empty') . ".</div><br />\n";
}//End if
foreach($globals as $varName => $super)
{
if(!is_array($super))
{
__printRow($varName, $super, 0);
}//End if
elseif(!empty($super))
{
ksort($super);
$varName = ucfirst(strtolower(substr($varName, 1)));
echo _tab(3) . "<div class=\"_v_table\">\n" .
_tab(4) . "<span class=\"_v _v_title\" id=\"_v_$varName" . "_title\">$varName</span>\n" .
_tab(5) . "<table class=\"_v _v_properties\" id=\"_v$varName\">\n";
$row = 0;
foreach($super as $key => $value)
{
__printRow($key, $value, $row++);
}//End foreach
echo _tab(4) . "</table>\n" .
_tab(3) . "</div>\n";
}//End if
}//End foreach
echo _tab(2) . "</div>\n";
}//End __v()
Which I've been testing using:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
._v { font-family: 'Tw Cen MT', serif; }
._v_container { clear: both; }
._v_key { text-align: right; }
._v_length { font-style: italic; }
._v_properties { column-width: auto; }
._v_row_alt_0 { background-color: rgba(99, 184, 230, 0.15); }
._v_table { float: left; }
._v_title, ._v_info_var_name { font-weight: bold; }
._v_type
{
width: 1px;
white-space: nowrap;
color: #575757;
text-align: center;
}
._v_type_boolean, ._v_type_null { color: #0000E6; }
._v_type_double { color: #f57900; }
._v_type_integer { color: #4e9a06; }
._v_type_string { color: #cc0000; }
</style>
</head>
<body>
<?php
/* include statement for file containing the above PHP code */
session_start();
$_SESSION['myArray'] = [true, [3.14159, 'Bishop'], [null], 42];
___v();
?>
</body>
</html>
Issues
- Table is improperly formed if the first element is not an array (although doesn't happen in this case)
- No handling for
objectnorresourcetypes, which I've yet to decide how to represent (suggestions welcome)
What I have so far seems to work fine, apart from the aforementioned issue, though the amount of code for what sounds like such a simple function is far above what I'd hoped, so I'd really appreciate any ideas on how to reduce it.