Here is a bit of code that displays: ‘varname: varvalue’ in a browser. It is used by calling the vc(‘varname’); or vc(array(‘multiple’,’varnames’));
I first tried writing:
if (gettype($$var_string) == ('string' || 'boolean' || 'integer' || 'double')){}
however this didn’t work. The second group simplifies to true, and it always passed the conditional check.
So, I wrote out each check separately. I’m new to coding, I’m sure there’s an easier way, not sure what it would be. I haven’t had any luck searching here or google. (probably not using the right jargon). Thanks in advance. – Mike
Oh, this is PHP, btw. The problem in question applies to any language thou.
define('LB', '<br />');
$var1 = 5;
$var2 = array('mike', 'lewis', 'is', 'awesome');
function vc($var_string) {
if (!is_array($var_string)){
$var_string = array($var_string);
}
foreach ($var_string as $var_string){
global $$var_string;
if ( (gettype($$var_string) == 'string') ||
(gettype($$var_string) == 'boolean') ||
(gettype($$var_string) == 'integer') ||
(gettype($$var_string) == 'double') ) {
print('<strong>'. $var_string . ': </strong>' . $$var_string . LB);
} elseif ( (gettype($$var_string) == 'array') ||
(gettype($$var_string) == 'object') ||
(gettype($$var_string) == 'resource')){
print('<strong>' . $var_string . ': </strong>');
print_r($$var_string);
print(LB);
}
}
}
vc('var1');
vc('var2');
vc(array('var1', 'var2'));
You can do something like this: