<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: variable code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 02:05:07 GMT</pubDate>
    <description>DZone Snippets: variable code</description>
    <item>
      <title>Output JavaScript variables from PHP</title>
      <link>http://snippets.dzone.com/posts/show/5342</link>
      <description>Class with useful static methods for outputting PHP values into JavaScript format.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com&lt;br /&gt;&lt;br /&gt;class JS{&lt;br /&gt;	//generic and maybe not the desired results xD&lt;br /&gt;	function value($o){&lt;br /&gt;		if($o === null)&lt;br /&gt;			return 'null';&lt;br /&gt;		$t = strtolower(gettype($o));&lt;br /&gt;		if($t == 'string' &amp;&amp; is_numeric($o) &amp;&amp; ($o[0] || strlen($o) == 1) || in_array($t, array('double', 'integer')))&lt;br /&gt;			$t = 'number';&lt;br /&gt;		elseif($t == 'string' &amp;&amp; preg_match('@^\d{4}(?:-\d{1,2}){1,2}(?: (?:\d{1,2}:){2}\d{1,2})?$@', $o)) //strtotime works also with "strange" values strtotime('x')&lt;br /&gt;			$t = 'date';&lt;br /&gt;		elseif($t == 'array' &amp;&amp; ($c = count($k = array_keys($o))) &amp;&amp; $k !== range(0, $c - 1))&lt;br /&gt;			$t = 'object';&lt;br /&gt;		elseif(!in_array($t, array('boolean', 'string', 'array', 'object')))&lt;br /&gt;			$t = 'string';&lt;br /&gt;		$t = 'from' . $t;&lt;br /&gt;		return self::$t($o);&lt;br /&gt;	}&lt;br /&gt;	function fromNumber($o){&lt;br /&gt;		return +$o . '';&lt;br /&gt;	}&lt;br /&gt;	function fromObject($o){&lt;br /&gt;		$r = array();&lt;br /&gt;		foreach($o as $n =&gt; $v)&lt;br /&gt;			$r[] = self::fromString($n) . ':' . self::value($v);&lt;br /&gt;		return '{' . implode(',', $r) . '}';&lt;br /&gt;	}&lt;br /&gt;	function fromBoolean($o){&lt;br /&gt;		return $o ? 'true' : 'false';&lt;br /&gt;	}&lt;br /&gt;	//$q = should quote? &lt;br /&gt;	//$c = char that will be used to quote&lt;br /&gt;	function fromString($o, $q = true, $c = '"'){&lt;br /&gt;		return ($p = $q ? $c : '') . preg_replace('/\r\n|\n\r|\r/', '\n', str_replace($c, '\\' . $c, str_replace('\\', '\\\\', $o))) . $p;&lt;br /&gt;	}&lt;br /&gt;	function fromArray($o){&lt;br /&gt;		$s = '';&lt;br /&gt;		foreach($o as $v)&lt;br /&gt;			$s .= ($s ? ',' : '') . self::value($v);&lt;br /&gt;		return '[' . $s . ']';&lt;br /&gt;	}&lt;br /&gt;	function fromDate($o){&lt;br /&gt;		(is_numeric($o) &amp;&amp; $o = +$o) || ($o = strtotime($o)) &gt; 0 || ($o = mktime());&lt;br /&gt;		$o = explode(',', date('Y,n,j,G,i,s', $o));&lt;br /&gt;		foreach($o as $i =&gt; $v)&lt;br /&gt;			$o[$i] = +$v;&lt;br /&gt;		return 'new Date(' . implode(',', $o)  . ')';&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$o = new stdClass;&lt;br /&gt;$o-&gt;abc = 123;&lt;br /&gt;echo implode("\n&lt;br /&gt;", array(&lt;br /&gt;	JS::value('1984-07-22 11:30:12'),&lt;br /&gt;	JS::value('Test'),&lt;br /&gt;	JS::value(1234),&lt;br /&gt;	JS::value(true),&lt;br /&gt;	JS::value(array(1,2,3)),&lt;br /&gt;	JS::value(array('lala' =&gt; 'x')),&lt;br /&gt;	JS::value($o)&lt;br /&gt;));&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 09 Apr 2008 23:57:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5342</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>Setting a string variable with a mult-line text value</title>
      <link>http://snippets.dzone.com/posts/show/4956</link>
      <description>This example demonstrates how easy it is to copy and paste human-readable text from elsewhere and declare it as a string in your code. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;string = &lt;&lt;RUBY_RUBY_RUBY&lt;br /&gt;  &lt;mydoc&gt;&lt;br /&gt;    &lt;someelement attribute="nanoo"&gt;Text, text, text&lt;/someelement&gt;&lt;br /&gt;  &lt;/mydoc&gt;&lt;br /&gt;RUBY_RUBY_RUBY&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note: Remember to encase the text within a pair of strings, any string (the convention is to make it uppercase) and ensure it's not a reserved keyword, or a variable declared already. </description>
      <pubDate>Mon, 07 Jan 2008 16:54:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4956</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>JavaScript var_dump (Mark 2)</title>
      <link>http://snippets.dzone.com/posts/show/4296</link>
      <description>Same as var_dump for PHP, but for JavaScript.  Useful if you do not have Firebug.&lt;br /&gt;&lt;br /&gt;A typical useage:&lt;br /&gt;&lt;br /&gt;document.write(var_dump(ANY-JS-VAR,'html'));&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;     function var_dump(data,addwhitespace,safety,level) {&lt;br /&gt;        var rtrn = '';&lt;br /&gt;        var dt,it,spaces = '';&lt;br /&gt;        if(!level) {level = 1;}&lt;br /&gt;        for(var i=0; i&lt;level; i++) {&lt;br /&gt;           spaces += '   ';&lt;br /&gt;        }//end for i&lt;level&lt;br /&gt;        if(typeof(data) != 'object') {&lt;br /&gt;           dt = data;&lt;br /&gt;           if(typeof(data) == 'string') {&lt;br /&gt;              if(addwhitespace == 'html') {&lt;br /&gt;                 dt = dt.replace(/&amp;/g,'&amp;amp;');&lt;br /&gt;                 dt = dt.replace(/&gt;/g,'&amp;gt;');&lt;br /&gt;                 dt = dt.replace(/&lt;/g,'&amp;lt;');&lt;br /&gt;              }//end if addwhitespace == html&lt;br /&gt;              dt = dt.replace(/\"/g,'\"');&lt;br /&gt;              dt = '"' + dt + '"';&lt;br /&gt;           }//end if typeof == string&lt;br /&gt;           if(typeof(data) == 'function' &amp;&amp; addwhitespace) {&lt;br /&gt;              dt = new String(dt).replace(/\n/g,"\n"+spaces);&lt;br /&gt;              if(addwhitespace == 'html') {&lt;br /&gt;                 dt = dt.replace(/&amp;/g,'&amp;amp;');&lt;br /&gt;                 dt = dt.replace(/&gt;/g,'&amp;gt;');&lt;br /&gt;                 dt = dt.replace(/&lt;/g,'&amp;lt;');&lt;br /&gt;              }//end if addwhitespace == html&lt;br /&gt;           }//end if typeof == function&lt;br /&gt;           if(typeof(data) == 'undefined') {&lt;br /&gt;              dt = 'undefined';&lt;br /&gt;           }//end if typeof == undefined&lt;br /&gt;           if(addwhitespace == 'html') {&lt;br /&gt;              if(typeof(dt) != 'string') {&lt;br /&gt;                 dt = new String(dt);&lt;br /&gt;              }//end typeof != string&lt;br /&gt;              dt = dt.replace(/ /g,"&amp;nbsp;").replace(/\n/g,"&lt;br&gt;");&lt;br /&gt;           }//end if addwhitespace == html&lt;br /&gt;           return dt;&lt;br /&gt;        }//end if typeof != object &amp;&amp; != array&lt;br /&gt;        for (var x in data) {&lt;br /&gt;           if(safety &amp;&amp; (level &gt; safety)) {&lt;br /&gt;              dt = '*RECURSION*';&lt;br /&gt;           } else {&lt;br /&gt;              try {&lt;br /&gt;                 dt = var_dump(data[x],addwhitespace,safety,level+1);&lt;br /&gt;              } catch (e) {continue;}&lt;br /&gt;           }//end if-else level &gt; safety&lt;br /&gt;           it = var_dump(x,addwhitespace,safety,level+1);&lt;br /&gt;           rtrn += it + ':' + dt + ',';&lt;br /&gt;           if(addwhitespace) {&lt;br /&gt;              rtrn += '\n'+spaces;&lt;br /&gt;           }//end if addwhitespace&lt;br /&gt;        }//end for...in&lt;br /&gt;        if(addwhitespace) {&lt;br /&gt;           rtrn = '{\n' + spaces + rtrn.substr(0,rtrn.length-(2+(level*3))) + '\n' + spaces.substr(0,spaces.length-3) + '}';&lt;br /&gt;        } else {&lt;br /&gt;           rtrn = '{' + rtrn.substr(0,rtrn.length-1) + '}';&lt;br /&gt;        }//end if-else addwhitespace&lt;br /&gt;        if(addwhitespace == 'html') {&lt;br /&gt;           rtrn = rtrn.replace(/ /g,"&amp;nbsp;").replace(/\n/g,"&lt;br&gt;");&lt;br /&gt;        }//end if addwhitespace == html&lt;br /&gt;        return rtrn;&lt;br /&gt;     }//end function var_dump&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 12 Jul 2007 15:29:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4296</guid>
      <author>singpolyma ()</author>
    </item>
    <item>
      <title>PHP variable check</title>
      <link>http://snippets.dzone.com/posts/show/3235</link>
      <description>ugh&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$page = isset($_GET['page']) ? $_GET['page'] : 'home';&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 03 Jan 2007 13:09:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3235</guid>
      <author>alexwilliams (Alex Williams)</author>
    </item>
    <item>
      <title>Python - Simple Example CGI</title>
      <link>http://snippets.dzone.com/posts/show/3122</link>
      <description>// http://localhost/cgi-bin/example.py?variable=example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import cgi&lt;br /&gt;&lt;br /&gt;print 'Content-type: text/html\r\n'&lt;br /&gt;&lt;br /&gt;inputValue = cgi.FieldStorage()&lt;br /&gt;&lt;br /&gt;if(inputValue.has_key('variable')):&lt;br /&gt;  print inputValue['variable'].value&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Dec 2006 16:48:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3122</guid>
      <author>whitetiger ()</author>
    </item>
    <item>
      <title>Ruby - Trace Variable Global</title>
      <link>http://snippets.dzone.com/posts/show/3036</link>
      <description>// Tracciare variabili Globali&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;trace_var:$variabileGlobale, proc{ print "variabileGlobale modificata al valore --&gt; ", $variabileGlobale, " "}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 25 Nov 2006 19:47:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3036</guid>
      <author>whitetiger ()</author>
    </item>
    <item>
      <title>parameterized instance variables</title>
      <link>http://snippets.dzone.com/posts/show/1082</link>
      <description>// useful when you want to obtain more than one instance of the same type of variable on the &lt;br /&gt;// same page. i.e. two different addresses. credit goes to argv[0] in #rubyonrails on freenode &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;instance_variable_get("@#{address_type}_address").country.name&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 04:24:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1082</guid>
      <author>mkovacs ()</author>
    </item>
    <item>
      <title>var_dump for javascript</title>
      <link>http://snippets.dzone.com/posts/show/759</link>
      <description>hackish implementation of the php 'var_dump()' in javascript:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function var_dump(obj) {&lt;br /&gt;   if(typeof obj == "object") {&lt;br /&gt;      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;&lt;br /&gt;   } else {&lt;br /&gt;      return "Type: "+typeof(obj)+"\nValue: "+obj;&lt;br /&gt;   }&lt;br /&gt;}//end function var_dump&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 21 Sep 2005 23:31:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/759</guid>
      <author>singpolyma ()</author>
    </item>
    <item>
      <title>change integer variables content</title>
      <link>http://snippets.dzone.com/posts/show/441</link>
      <description>&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;#include&lt;conio.h&gt;&lt;br /&gt;void main(){&lt;br /&gt;	int a, b;&lt;br /&gt;	clrscr();&lt;br /&gt;&lt;br /&gt;	printf( "Digite a= " );&lt;br /&gt;	scanf( "%d", &amp;a );&lt;br /&gt;&lt;br /&gt;	printf( "Digite b= " );&lt;br /&gt;	scanf( "%d", &amp;b );&lt;br /&gt;&lt;br /&gt;	a = b - a + ( b = a );&lt;br /&gt;&lt;br /&gt;	printf( "a= %d\nb= %d", a, b);&lt;br /&gt;	getch();&lt;br /&gt;}&lt;br /&gt;*&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jul 2005 04:02:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/441</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
