Note that there are some explanatory texts on larger screens.

plurals
  1. POConditional logic optimization: How to check against multiple unique cases in 1 line?
    primarykey
    data
    text
    <p>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'));</p> <p>I first tried writing: </p> <pre><code>if (gettype($$var_string) == ('string' || 'boolean' || 'integer' || 'double')){} </code></pre> <p>however this didn't work. The second group simplifies to true, and it always passed the conditional check.</p> <p>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</p> <p>Oh, this is PHP, btw. The problem in question applies to any language thou.</p> <pre><code>define('LB', '&lt;br /&gt;'); $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('&lt;strong&gt;'. $var_string . ': &lt;/strong&gt;' . $$var_string . LB); } elseif ( (gettype($$var_string) == 'array') || (gettype($$var_string) == 'object') || (gettype($$var_string) == 'resource')){ print('&lt;strong&gt;' . $var_string . ': &lt;/strong&gt;'); print_r($$var_string); print(LB); } } } vc('var1'); vc('var2'); vc(array('var1', 'var2')); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload