<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scriptNode</title>
	<atom:link href="http://scriptnode.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://scriptnode.com</link>
	<description>Tips and tricks for web developers.</description>
	<lastBuildDate>Tue, 08 Sep 2009 20:31:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tabs VS Spaces</title>
		<link>http://scriptnode.com/article/tabs-vs-spaces/</link>
		<comments>http://scriptnode.com/article/tabs-vs-spaces/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 19:59:20 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Novice]]></category>
		<category><![CDATA[code-style]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=353</guid>
		<description><![CDATA[
Let&#8217;s get this right out in the open regarding indentation: tabs are correct and spaces are wrong. Now that we&#8217;ve got that over with, let&#8217;s go into the why of it:

Tabs are more flexible

When code is using spaces for indentation, the viewer is rigidly locked into viewing the code with that exact format. Some programmers [...]]]></description>
			<content:encoded><![CDATA[<p>
Let&#8217;s get this right out in the open regarding indentation: <strong>tabs are correct</strong> and <strong>spaces are wrong</strong>. Now that we&#8217;ve got that over with, let&#8217;s go into the <em>why</em> of it:
</p>
<h3>Tabs are more flexible</h3>
<p>
When code is using spaces for indentation, the viewer is rigidly locked into viewing the code with that exact format. Some programmers might want to see indentation as two spaces, others might want four. These are both reasonable requests, and it should be trivial to satisfy both requirements.
</p>
<p>
It&#8217;s very simple for almost any editor to display a tab at whatever width the user desires. Spaces do not provide the ability to do this. Therefore, tabs are more appropriate.
</p>
<h3>Tabs have semantic meaning</h3>
<p>
When referring to indentation, a tab is the perfect representation. A space is typically a separator between words, sentences, punctuation, variables, operators or other list items. It can be many different things, but typically is just a buffer to allow similar items to breathe. When placing a dozen or so of these in front of a line of code, you&#8217;re separating separators with other separators, which isn&#8217;t useful.
</p>
<p>
When using tabs instead, not only are there less characters being used, but a tab is also representing an indentation, which makes perfect semantic sense.
</p>
<h3>&#8220;But tabs mess up multi-line assignments!&#8221;</h3>
<p>
This is a common complaint that I hear about tabs. Here&#8217;s an example of what this means:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
var foo               = &#8216;bar&#8217;;<br />
var a_really_long_foo = &#8216;bar&#8217;;<br />
var another_foo       = &#8216;bar&#8217;;<br />
</textarea></p>
<p>
The complaint is that when formatting code like this, tabs viewed at two spaces look one way and tabs viewed at four spaces look a completely different way. What really blows my mind about this argument is that <strong>spaces are actually appropriate for this format</strong> and it&#8217;s such a trivial thing to hit <code>space</code> instead of <code>tab</code> when formatting your code like this. That&#8217;s not much of a compromise.
</p>
<h3>&#8220;But I want people to see my code the way I want them to see it!&#8221;</h3>
<p>
Then you&#8217;re selfish. Please realize this. Would you insist on whoever&#8217;s reading your code to use a specific syntax highlighting color scheme? Of course not. If for some reason you like tiny fonts, would you insist that those with poor eye sight view your code in the same format? I really hope not. When <em>freedom</em> or <em>strict, arbitrary rules</em> are your two choices, I really hope you&#8217;ll appease everyone involved and see the light.
</p>
<h3>Consider the extremes</h3>
<p>
If you care at all about this discussion, then I&#8217;m guessing you&#8217;ve worked with programmers before. You know that they tend to be intense, obsessive people, often with preferences that seem weird to others. I&#8217;ve heard of some programmers preferring as low as one character indentation, and others who prefer up to eight. See how different that looks:
</p>
<h4>One character indentation</h4>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
if (obj) {<br />
 for (i in obj) {<br />
  if (obj[i]) {<br />
   obj[i]();<br />
  }<br />
 }<br />
}<br />
</textarea></p>
<h4>Eight character indentation</h4>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
if (obj) {<br />
        for (i in obj) {<br />
                if (obj[i]) {<br />
                        obj[i]();<br />
                }<br />
        }<br />
}<br />
</textarea></p>
<p>
You might not prefer either extreme, but try to remember that we&#8217;re not all the same. We each have our own, often bizarre preferences. Tabs ensure that everyone is happy.
</p>
<h3>Summary</h3>
<p>
We discussed why tabs are the correct choice and spaces are incorrect. Tabs are flexible and give each reader the ability to view them the way s/he chooses. They also have semantic meaning that can be useful for a myriad of reasons.
</p>
<p>
So please keep this in mind when making a decision on your codebase: spaces are rigid and uncompromising, meaning <strong>spaces are a selfish, stubborn choice</strong>. Tabs are flexible, which means <strong>tabs provide the freedom programmers need</strong> to view the code in their own format. Make the right choice!</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/tabs-vs-spaces/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making @Twitter Names Clickable</title>
		<link>http://scriptnode.com/article/making-twitter-names-clickable/</link>
		<comments>http://scriptnode.com/article/making-twitter-names-clickable/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 07:10:32 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Novice]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[script-sunday]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=334</guid>
		<description><![CDATA[
Here&#8217;s a super quick Script Sunday for you: making @Twitter usernames clickable. Here&#8217;s an example:


@scriptnode

Should be:

@&#60;href="http://twitter.com/scriptnode"&#62;scriptnode&#60;/a&#62;

Which looks like:

@scriptnode


And here&#8217;s how to do it in PHP and JavaScript:

PHP

$text = preg_replace(&#8217;#@([a-z0-9_]+)#i&#8217;, &#8216;@&#60;a href=&#8221;http://twitter.com/\\1&#8243;&#62;\\1&#60;/a&#62;&#8217;, $text);


That was easy! Now let&#8217;s get to the more interesting JavaScript, which allows us to test inline!

JavaScript

text = text.replace(/@([a-z0-9_]+)/gi, &#8216;@&#60;a href=&#8221;http://twitter.com/$1&#8243;&#62;$1&#60;/a&#62;&#8217;);


You can run this [...]]]></description>
			<content:encoded><![CDATA[<p>
Here&#8217;s a super quick Script Sunday for you: making @Twitter usernames clickable. Here&#8217;s an example:
</p>
<p>
<code>@scriptnode</code>
</p>
<p>Should be:</p>
<p>
<code>@&lt;href="http://twitter.com/scriptnode"&gt;scriptnode&lt;/a&gt;</code>
</p>
<p>Which looks like:</p>
<p>
@<a href="http://twitter.com/scriptnode">scriptnode</a>
</p>
<p>
And here&#8217;s how to do it in PHP and JavaScript:
</p>
<h3>PHP</h3>
<p><textarea class="php" cols="50" name="code" rows="10"><br />
$text = preg_replace(&#8217;#@([a-z0-9_]+)#i&#8217;, &#8216;@&lt;a href=&#8221;http://twitter.com/\\1&#8243;&gt;\\1&lt;/a&gt;&#8217;, $text);<br />
</textarea></p>
<p>
That was easy! Now let&#8217;s get to the more interesting JavaScript, which allows us to test inline!
</p>
<h3>JavaScript</h3>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
text = text.replace(/@([a-z0-9_]+)/gi, &#8216;@&lt;a href=&#8221;http://twitter.com/$1&#8243;&gt;$1&lt;/a&gt;&#8217;);<br />
</textarea></p>
<p>
You can run this code right here:
</p>
<div class="section">
<p id="example-twitter-output">Some @<a href="http://twitter.com/test">test</a> code</p>
<input id="example-twitter-input" type="text" value="Some @test code">
</div>
<p>
<button onclick="YAHOO.twitterExample();">Format</button>
</p>
<p><script type="text/javascript">
<!--//
YAHOO.twitterExample = function() {
var text = document.getElementById('example-twitter-input').value;
text = text.replace(/@([a-z0-9_]+)/gi, '@<a href="http://twitter.com/$1">$1</a>');
document.getElementById('example-twitter-output').innerHTML = text;
};
//-->
</script></p>
<p>
I&#8217;m awful at regular expressions; please do let me know if I could optimize somewhere or if I missed something.<br />
<strong>Edit:</strong> <code>+</code> instead of <code>*</code></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/making-twitter-names-clickable/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP: goto hell;</title>
		<link>http://scriptnode.com/article/php-goto-hell/</link>
		<comments>http://scriptnode.com/article/php-goto-hell/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 19:53:11 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Novice]]></category>
		<category><![CDATA[code-style]]></category>
		<category><![CDATA[just-for-fun]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=326</guid>
		<description><![CDATA[
As of version 5.3, PHP supports goto (much to the chagrin of many). To protest, one snarky programmer filed a faux bug in their bug tracking system:





Funny stuff! But all kidding aside, I personally feel like PHP was ugly enough without goto. To me, one of the language&#8217;s biggest downfalls has always been that it [...]]]></description>
			<content:encoded><![CDATA[<p>
As of <a href="http://www.sitepoint.com/article/whats-new-php-5-3/">version 5.3</a>, PHP supports <a href="http://us3.php.net/goto"><code>goto</code></a> (much to the chagrin of many). To protest, one snarky programmer filed a <a href="http://bugs.php.net/bug.php?id=48669">faux bug</a> in their bug tracking system:
</p>
<p>
<a href="http://bugs.php.net/bug.php?id=48669"><img alt="PHP: goto hell;" src="/assets/img/php_goto_hell.jpg" style="border: 1px solid #000;"/></a>
</p>
<p>
Funny stuff! But all kidding aside, I personally feel like PHP was ugly enough without <code>goto</code>. To me, one of the language&#8217;s biggest downfalls has always been that it practically <em>encourages</em> sloppy design, and this is a huge step in the wrong direction. How do you feel about it?
</p>
<p>
(Link via <a href="http://foohack.com/">Isaac Schlueter</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/php-goto-hell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>5 Things That Suck About Being a Frontend Engineer</title>
		<link>http://scriptnode.com/article/5-things-that-suck-about-being-a-frontend-engineer/</link>
		<comments>http://scriptnode.com/article/5-things-that-suck-about-being-a-frontend-engineer/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 00:23:18 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Novice]]></category>
		<category><![CDATA[careers]]></category>
		<category><![CDATA[just-for-fun]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=313</guid>
		<description><![CDATA[
Call it what you will. There&#8217;s been much debate about the job title for a frontend engineer, or a web developer, or whatever name you prefer. What I&#8217;m referring to, however, are the folks who code for the browser. If you spend all day waste-deep in markup, CSS and JavaScript, then you do what I [...]]]></description>
			<content:encoded><![CDATA[<p>
Call it what you will. There&#8217;s been much debate about the job title for a frontend engineer, or a web developer, or whatever name you prefer. What I&#8217;m referring to, however, are the folks who code for the browser. If you spend all day waste-deep in markup, <a href="/tag/css/">CSS</a> and <a href="/tag/javascript/">JavaScript</a>, then you do <a href="http://raptr.com/">what I do</a> and I&#8217;m talking about you.
</p>
<p>
And let&#8217;s just skip right past <a href="http://www.stopie6.com/">Internet Explorer</a> and its plethora of headaches, shall we? We all know it sucks. Here&#8217;s why <em>else</em> it sucks to be one of us:
</p>
<ol>
<li>
<h3>Everyone Thinks It&#8217;s Your Fault</h3>
<p>
So you&#8217;re <a href="/article/5-ways-qa-ruin-developers-lives/">QA</a>. You&#8217;re testing a website and something is broken. It looks to you like the frontend, because that&#8217;s what you see. What may separate good QA from excellent QA is the ability to determine where the error lies, but sometimes that&#8217;s out of QA&#8217;s scope.
</p>
<p>
Regardless of who the owner is, chances are the issue will go to you first. You must then determine if it&#8217;s even your issue, and either fix it, or pass it on. Bringing us to …
</p>
</li>
<li>
<h3>You Are Also a Project Manager</h3>
<p>
The frontend is the very <strong>last</strong> thing to get done. That&#8217;s because the design needs to be finished way in advance so that assets can be delivered, and the backend has to be complete before you have access to data to use in your implementation.
</p>
<p>
This means that you must act as <strong>Project Manager</strong> to keep everyone else on track, because otherwise you can&#8217;t ever get to work! And that&#8217;s why …
</p>
</li>
<li>
<h3>You Always Seem Late</h3>
<p>
Since you&#8217;re the last one that gets to begin implementing, chances are you&#8217;ve got less time than everyone else. And sometimes you can&#8217;t even get started until <em>after</em> your deadline! And even when you do finally get your assets and can get to it, sometimes …
</p>
</li>
<li>
<h3>You Must Know Photoshop</h3>
<p>
To be fair, I actually use <a href="http://www.gimp.org/">The Gimp</a>, which works fine. What I mean by this point is that you must learn to use a competent image manipulation program. Why? Because although you may have an excellent design person/team, chances are they&#8217;re not going to cater to you and your crazy <a href="http://delicious.com/richtaur/css">CSS hacks</a>.
</p>
<p>
You know what I mean. Sometimes you&#8217;re wanting to <a href="http://developer.yahoo.com/performance/rules.html">sprite your images</a> to get better page performance, or sometimes you need to pull off some crazy rounded corners hack to get the positioning <a href="http://developer.yahoo.com/yui/articles/gbs/">cohesive across browsers</a>. You could go back-and-forth with design all day, or you could say &#8220;screw it&#8221; and alter the image yourself. It sucks but that&#8217;s how it gets done. And that&#8217;s part of …
</p>
</li>
<li>
<h3>You Must be a Jack-of-All-Trades</h3>
<p>
In addition to competency with a decent image manipulation application, you&#8217;re going to need at least a familiarity in many other areas. It&#8217;s very rare to find a frontend engineer who doesn&#8217;t ever need to modify at least some server-side code.
</p>
<p>
For example, although I am official a frontend engineer, I probably spend 30-50% of my time in <a href="/tag/php/">PHP</a>. That&#8217;s because, before I can even render the markup I need to tweak, I&#8217;ve got to setup the data I receive from the backend.
</p>
<p>
Not to mention the various other tasks that randomly pop up in the webdev stack, such as <a href="http://www.addedbytes.com/apache/url-rewriting-for-beginners/">Apache rewrite rules</a> or cronjobs. I&#8217;m sure the same could be said for most jobs, but it seems like frontend engineers&#8217; skillsets must be broader than most.
</p>
</li>
</ul>
<div class="section">
But it&#8217;s not all bad. Especially right now in Silicon Valley, frontend engineers are quite valuable, so it&#8217;s a great time to know how to code for the browser. Either way, did I miss something? Let me know!
</div>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/5-things-that-suck-about-being-a-frontend-engineer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple Loading Bar</title>
		<link>http://scriptnode.com/article/simple-loading-bar/</link>
		<comments>http://scriptnode.com/article/simple-loading-bar/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 06:56:36 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Novice]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lab]]></category>
		<category><![CDATA[script-sunday]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=316</guid>
		<description><![CDATA[
Haven&#8217;t had a Script Sunday for a while, so here&#8217;s a quick, simple script for you. Ever see those Loading … overlay images and wonder if JavaScript can handle it? Turns out it&#8217;s extremely simple:


animate = (function() {
  var busy;
  return function(id) {
    if (busy) return;
    busy [...]]]></description>
			<content:encoded><![CDATA[<p>
Haven&#8217;t had a <a href="/tag/script-sunday/">Script Sunday</a> for a while, so here&#8217;s a quick, simple script for you. Ever see those <strong>Loading …</strong> overlay images and wonder if JavaScript can handle it? Turns out it&#8217;s extremely simple:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
animate = (function() {</p>
<p>  var busy;</p>
<p>  return function(id) {</p>
<p>    if (busy) return;</p>
<p>    busy = true;<br />
    var width = 0;<br />
    var interval = setInterval(function() {</p>
<p>      var el = document.getElementById(id);<br />
      el.innerHTML = &#8216;@%
<div style="width: @px;"><span>@%</span></div>
<p>&#8216;.replace(/@/g, width);</p>
<p>      if (width++ >= 100) {<br />
        busy = false;<br />
        clearInterval(interval);<br />
      }</p>
<p>    }, 50);</p>
<p>  };</p>
<p>})();<br />
</textarea></p>
<p>
You can view this by itself <a href="/lab/bar/">in the lab</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/simple-loading-bar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSQL: JavaScript Query Language</title>
		<link>http://scriptnode.com/article/jsql-javascript-query-language/</link>
		<comments>http://scriptnode.com/article/jsql-javascript-query-language/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 06:12:20 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[lab]]></category>
		<category><![CDATA[script-sunday]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=286</guid>
		<description><![CDATA[About JSQL

When I started my new job, I had to make the transition from YUI to jQuery. I&#8217;d seen some jQuery code before (in fact, its terseness and use of $ as both an object and a function were easy to spot), but I wasn&#8217;t familiar with it. I actually thought that the query part [...]]]></description>
			<content:encoded><![CDATA[<h3>About JSQL</h3>
<p>
When I started <a href="http://richter.paletteswap.com/why-i-left-yahoo-for-a-startup/">my new job</a>, I had to make the transition from <a href="http://developer.yahoo.com/yui/">YUI</a> to <a href="http://jquery.com/">jQuery</a>. I&#8217;d seen some jQuery code before (in fact, its terseness and use of <code>$</code> as both an object and a function were easy to spot), but I wasn&#8217;t familiar with it. I actually thought that the <em>query</em> part of jQuery meant there was some kind of <a href="http://mysql.com/">Standard Query Language</a> interface. There isn&#8217;t, but there is a really cool <a href="http://www.css3.info/">CSS3</a> selector instead.
</p>
<p>
Turns out, I was a little disappointed; I wanted to use something query-based. So, I started to throw this script together. I call it <acronym title="JavaScript Query Language">JSQL</acronym>. Below are some quick examples of what it looks like in action:
</p>
<p><script src="/assets/js/jsql/jsql-min.js" type="text/javascript"></script></p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
var results = JSQL(&#8217;SELECT * FROM #ft WHERE nodeName = &#8220;LI&#8221;);<br />
</textarea></p>
<p>
JSQL always returns an object with an <code>error</code> property that&#8217;s either <code>Boolean false</code> or a <code>String</code> stating what error occurred (for example, &#8220;Unknown command UPDAT3&#8243;). If it successfully retrieves or operates on elements, it returns a <code>count:Number</code> property with the number of elements and an <code>elements:Array</code> property with those elements.
</p>
<p>
Here are some interactive examples:
</p>
<h3>Example 1: Checkboxes</h3>
<div class="section" id="jsql-example-1">
<div>
<input type="checkbox" value=""/> Option number 1
		</div>
<div>
<input type="checkbox" value=""/> Option number 2
		</div>
<div>
<input type="checkbox" value=""/> Option number 3
		</div>
</p></form>
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-1 SET checked = true WHERE type = &#8220;checkbox&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-1">Set Checked</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-1').onclick = function() {
JSQL('UPDATE #jsql-example-1 SET checked = true WHERE type = "checkbox"');
};
</script><br />
<textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-1 SET checked = false WHERE type = &#8220;checkbox&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-1a">Uncheck</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-1a').onclick = function() {
JSQL('UPDATE #jsql-example-1 SET checked = false WHERE type = "checkbox"');
};
</script></p>
<h3>Example 2: Form Validation</h3>
<div class="section" id="jsql-example-2">
<p>
			First name:</p>
<input type="text" value=""/>
<p>
			Last name:</p>
<input type="text" value=""/>
<p>
			Email:</p>
<input type="text" value=""/>
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-2 SET parentNode.className = &#8220;error&#8221; WHERE value = &#8220;&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-2">Highlight empty fields</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-2').onclick = function() {
JSQL('UPDATE #jsql-example-2 SET parentNode.className = "error" WHERE value = ""');
};
</script><br />
<textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-2 SET parentNode.className = &#8220;&#8221; WHERE value = &#8220;&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-2a">Reset</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-2a').onclick = function() {
JSQL('UPDATE #jsql-example-2 SET parentNode.className = "" WHERE value = ""');
};
</script></p>
<h3>Example 3: Removing Elements</h3>
<div class="section" id="jsql-example-3">
<strong>Lorem</strong> Ipsum is simply dummy text of the <strong>printing and</strong> typesetting industry. <strong>Lorem</strong> Ipsum has been the <strong>industry&#8217;s</strong> standard dummy text <strong>ever</strong> since the 1500s, when an unknown printer took a galley of type and scrambled it to make a <strong>type specimen</strong> book. It has survived not only <strong>five</strong> centuries, but also the leap into <strong>electronic</strong> typesetting<strong>, remaining essentially unchanged</strong>. It was popularised in the 1960s with the release of <strong>Letraset</strong> sheets containing <strong>Lorem Ipsum</strong> passages, and more recently with <strong>desktop publishing</strong> software like Aldus PageMaker including versions of <strong>Lorem</strong> Ipsum.
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;DELETE FROM #jsql-example-3 WHERE nodeName = &#8220;STRONG&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-3">Remove strong tags</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-3').onclick = function() {
JSQL('DELETE FROM #jsql-example-3 WHERE nodeName = "STRONG"');
};
</script></p>
<p>
In the spirit of jQuery, I also stuck in a quick <code>each</code> method which you can use to iterate through the returned elements immediately instead of setting up your own loop. The context is the current element (like jQuery) and the return value is still the same:</p>
<h3>Example 4: Selecting Elements</h3>
<div class="section" id="jsql-example-4">
Give him that <em>gun</em>. Give him <em>ALL</em> the guns.
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8221;SELECT * FROM #jsql-example-4 WHERE nodeName = &#8216;EM&#8217;&#8221;).each(function() {<br />
	this.style.background = &#8216;#ffff00&#8242;;<br />
});<br />
</textarea></p>
<p>
<button id="jsql-button-4">Select &amp; Operate</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-4').onclick = function() {
JSQL("SELECT * FROM #jsql-example-4 WHERE nodeName = 'EM'").each(function() {
	this.style.background = '#ffff00';
});
};
</script><br />
<textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8221;SELECT * FROM #jsql-example-4 WHERE nodeName = &#8216;EM&#8217;&#8221;).each(function() {<br />
	this.style.background = &#8221;;<br />
});<br />
</textarea></p>
<p>
<button id="jsql-button-4a">Undo</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-4a').onclick = function() {
JSQL("SELECT * FROM #jsql-example-4 WHERE nodeName = 'EM'").each(function() {
	this.style.background = '';
});
};
</script></p>
<p>
I started coding this several months ago, and just recently decided to revisit it. I found the code hard to read myself when making some modifications before publishing it here. That typically means it&#8217;s poorly written. And I&#8217;m admittedly lousy at writing parsing code&#8230; so there&#8217;s that. I also doubt it&#8217;s realistic for any real-world websites (other libraries handle those jobs quite well), but I thought it would be fun to play with.
</p>
<p>
Some noticeable features lacking are equivalents to <code>ALTER</code> and <code>INSERT INTO</code>. I&#8217;ve also got it <a href="http://github.com/richtaur/jsql/tree/master">up on GitHub</a>, so feel free to fork it, and maybe add those methods yourself!
</p>
<ul>
<li><a href="/assets/js/jsql/jsql.js">[Download jsql.js]</a> (11k)</li>
<li><a href="/assets/js/jsql/jsql-min.js">[Download jsql-min.js]</a> (3,661 bytes)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/jsql-javascript-query-language/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Spacius! Source Code</title>
		<link>http://scriptnode.com/article/spacius-source-code/</link>
		<comments>http://scriptnode.com/article/spacius-source-code/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 05:52:23 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[code-style]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[just-for-fun]]></category>
		<category><![CDATA[lab]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=278</guid>
		<description><![CDATA[
I&#8217;ve been meaning to &#8220;release&#8221; this for quite some time now, but I wanted to do it in small doses, with in-depth explanations of the code. Making a game can be somewhat overwhelming, but it&#8217;s just like any other large task: if you break it up into smaller pieces, it becomes much easier to manage.


But [...]]]></description>
			<content:encoded><![CDATA[<p>
I&#8217;ve been meaning to &#8220;release&#8221; this for quite some time now, but I wanted to do it in small doses, with in-depth explanations of the code. Making a game can be somewhat overwhelming, but it&#8217;s just like any other large task: if you break it up into smaller pieces, it becomes much easier to manage.
</p>
<p>
But time is a commodity nowadays. I&#8217;ve got way less time to write, and when I do, I&#8217;d rather be writing <em>code</em> than tutorials (or honestly, <a href="http://raptr.com/richtaur"><em>playing</em> video games</a>). Sorry about that, but I hope the (well-commented!) source will help those of you out there who&#8217;d like to contribute to the <a href="http://games.nihilogic.dk/">slowly growing world of DHTML games</a>.
</p>
<p>
You&#8217;ll notice that this code is <a href="/article/spacius-a-space-adventure/">over 6 months old</a>. And I&#8217;m the kind of developer who constantly updates his <a href="/tag/code-style/">style, methods and patterns</a>. Given that, there may very well be many mistakes or bad practices in here, some of which I may have improved upon by now, but others maybe not. Either way, feel free to comment on them, and I&#8217;ll answer any questions as well.
</p>
<p>
Thanks for checking it out, and please be good to it. I&#8217;d love to know about any projects created with this source as a basis, and would appreciate links back here. Also please don&#8217;t claim it as your own, that&#8217;s not cool.
</p>
<div class="section">
For now I am hosting the source code with <a href="http://github.com/richtaur/spacius-/tree/master">GitHub</a>.
</div>
<p>
Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/spacius-source-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Seemingly Snappier User Interface</title>
		<link>http://scriptnode.com/article/a-seemingly-snappier-user-interface/</link>
		<comments>http://scriptnode.com/article/a-seemingly-snappier-user-interface/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 04:55:20 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=263</guid>
		<description><![CDATA[

Say you&#8217;ve got a fancy user interface. You&#8217;re leveraging AJAX well, you&#8217;re making good use of best practices, and you&#8217;ve got fast servers that respond quickly.
But your user actions still aren&#8217;t quite as fast as you&#8217;d like. What else can you do?


If you&#8217;ve done things correctly, you&#8217;ve got a user interface that reacts positively to [...]]]></description>
			<content:encoded><![CDATA[<p><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script></p>
<p>
Say you&#8217;ve got a fancy user interface. You&#8217;re leveraging <a href="http://ajaxian.com">AJAX</a> well, you&#8217;re making good use of <a href="http://yuiblog.com/">best practices</a>, and you&#8217;ve got fast servers that <a href="/tag/optimization/">respond quickly</a>.<br />
But your user actions still aren&#8217;t quite as fast as you&#8217;d like. What else can you do?
</p>
<p>
If you&#8217;ve done things correctly, you&#8217;ve got a user interface that reacts positively to successful flows, but will also handle errors gracefully.<br />
Even though you&#8217;re &#8220;doing it right,&#8221; consider restructuring your application: <strong>assume success</strong>. Normally, your server is up and running and operations<br />
will be completely successfully. The edge case (hopefully) is the error case. Assuming this, we can create a slightly faster user interface.
</p>
<p>
Let&#8217;s begin with a basic voting mechanism. You&#8217;ve got a control on your page that lets a user vote on an object, and a small bit of text to represent the state of said control.<br />
It may look something like this:
</p>
<div class="section">
	<button>Vote</button><br />
	<span>You haven&#8217;t voted yet</span>
</div>
<p>
This example is meant to be easy and functional; hopefully <strong>your</strong> controls are prettier.<br />
Regardless, here&#8217;s a common flow for this request and its callback states:
</p>
<ol>
<li>Listen for the <code>click</code> event on the control.</li>
<li>Send the request.</li>
<li>Display a <a href="http://ajaxload.info/">&#8220;loading&#8221; message or a typical spinner image</a>.
<li>If the call comes back successfully, provide &#8220;success&#8221; feedback to the user and update the control.</li>
<li>If the call throws an error, provide an error message.</li>
</ol>
<p>
This flow is <strong>extremely</strong> common in today&#8217;s web. But we want something more immediately responsive, so let&#8217;s restructure the flow:
</p>
<ol>
<li>
		Use <code>onmousedown</code> because it will fire off just slightly sooner than <code>onclick</code> would.<br />
		(This might not suit all user interfaces but in this case it&#8217;s fine, and it wins us a few milliseconds.)
	</li>
<li>Provide &#8220;success&#8221; feedback to the user and update the control.</li>
<li>Send the request.</li>
<li>If the call throws an error, reset the controls and provide an error message.</li>
</ol>
<p>
This structure might seem a little off. Indeed, the error case even &#8220;lies&#8221; to the user for a few milliseconds if the request fails.<br />
But it covers all of its bases; none of the previous flow is omitted.<br />
Keep in mind that <strong>the error case is the edge case</strong>, and a very large majority of the time it&#8217;s fine to assume success at first.
</p>
<p>
Using <a href="http://jquery.com/">jQuery</a>, here&#8217;s what the code might look like:
</p>
<p><textarea class="javascript" cols="10" name="code" rows="10"><br />
// Attach the mousedown event listener<br />
$(&#8217;#snappier-1-btn&#8217;).mousedown(function(e) {</p>
<p>	var el = $(this);</p>
<p>	// Go ahead and assume things went smoothly<br />
	el.attr(&#8217;disabled&#8217;, true);<br />
	el.next().html(&#8217;Voted!&#8217;);</p>
<p>	// Send the XHR<br />
	$.ajax({<br />
		error : function() {</p>
<p>			// Undo the success message and allow the user to repeat<br />
			el.attr(&#8217;disabled&#8217;, false);<br />
			el.next().html(&#8221;You haven&#8217;t voted yet&#8221;);</p>
<p>			// Let the user know what happened<br />
			alert(&#8221;Sorry, an error occurred! Please try again.&#8221;);</p>
<p>		},<br />
		type : &#8216;GET&#8217;,<br />
		url : &#8216;/feed/&#8217;<br />
	});</p>
<p>});<br />
</textarea></p>
<p>
And here&#8217;s this flow working right here on the page:
</p>
<div class="section">
	<button id="snappier-1-btn">Vote</button><br />
	<span id="snappier-1-msg">You haven&#8217;t voted yet</span>
</div>
<p><script type="text/javascript"></p>
<p>// Attach the mousedown event listener
$('#snappier-1-btn').mousedown(function(e) {</p>
<p>	var el = $(this);</p>
<p>	// Go ahead and assume things went smoothly
	el.attr('disabled', true);
	el.next().html('Voted!');</p>
<p>	// Send the XHR
	$.ajax({
		error : function() {</p>
<p>			// Undo the success message and allow the user to repeat
			el.attr('disabled', false);
			el.next().html("You haven't voted yet");</p>
<p>			// Let the user know what happened
			alert("Sorry, an error occurred! Please try again.");</p>
<p>		},
		type : 'GET',
		url : '/feed/'
	});</p>
<p>});</p>
<p></script></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/a-seemingly-snappier-user-interface/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Common PHP Equivalents in JavaScript</title>
		<link>http://scriptnode.com/article/common-php-equivalents-in-javascript/</link>
		<comments>http://scriptnode.com/article/common-php-equivalents-in-javascript/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 09:04:45 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[code-style]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=245</guid>
		<description><![CDATA[
It&#8217;s pretty widely known in the web development industry that server-side scripting is important, and PHP is an extremely common choice. Indeed, for many programmers it&#8217;s even  their first language. It&#8217;s quite common for web devs to begin on the server, then later require some client-side interaction, at which point they must turn to [...]]]></description>
			<content:encoded><![CDATA[<p>
It&#8217;s pretty widely known in the web development industry that server-side scripting is important, and <a href="http://us3.php.net/" title="PHP Hypertext Preprocessor">PHP</a> is an extremely common choice. Indeed, for many programmers <a href="http://reidburke.com/2008/10/05/when-php-is-your-first-language/">it&#8217;s even  their first language</a>. It&#8217;s quite common for web devs to begin on the server, then later require some client-side interaction, at which point they must turn to <a href="/tag/javascript/">JavaScript</a>.
</p>
<p>
I often see questions similar to, &#8220;I&#8217;ve got these few lines of PHP code, how would I do this in JavaScript?&#8221; I&#8217;ve actually <a href="/article/javascript-print_r-or-var_dump-equivalent/">already answered</a> a question like this, but I kept getting more questions, so I thought I&#8217;d cover many at once:
</p>
<ul>
<li><a href="#array-push">Pushing to an array</a></li>
<li><a href="#die"><code>die()</code> or <code>exit</code></a></li>
<li><a href="#include"><code>include</code> or <code>require</code></a></li>
<li><a href="#redirection">Redirection</a></li>
<li><a href="#static-classes">Static classes</a></li>
<li><a href="#strings">String manipulation</a></li>
</ul>
<h3 id="array-push">Pushing to an array</h3>
<p>
Pushing a new value to the end of an array is a common, trivial task. Here&#8217;s some PHP to accomplish this:
</p>
<p><textarea class="php" cols="50" name="code" rows="10"><br />
$my_array = array();<br />
$my_array[] = &#8216;a&#8217;;<br />
array_push($my_array, &#8216;b&#8217;);<br />
</textarea></p>
<p>
Also note this quick pointer from php.net&#8217;s <a href="http://us3.php.net/manual/en/function.array-push.php"><code>array_push()</code> documentation</a>:
</p>
<blockquote><p>If you use <code>array_push()</code> to add one element to the array it&#8217;s better to use <code>$array[] =</code> because in that way there is no overhead of calling a function.</p>
</blockquote>
<p>
This isn&#8217;t the case in JavaScript. The native <code>Array.push</code> method is most optimal:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
var myArray = [];<br />
myArray.push(&#8217;a'); // This is the way to go<br />
myArray[myArray.length] = &#8216;b&#8217;; // This method is possible but bad<br />
</textarea></p>
<h3 id="die"><code>die()</code> or <code>exit</code></h3>
<p>
There has been much confusion regarding what JavaScript is and how it works in the browser. When looking for an equivalent to <code>exit</code>, one probably doesn&#8217;t understand that client-side JavaScript is a behavioral language that&#8217;s intended to react to events. In PHP, a script executes, stopping only when told to do so, and then it&#8217;s done. JavaScript lives and breathes on the page, reacting to the mouse being moved, keys being pressed and even time changing.
</p>
<p>
So it&#8217;s important to understand why this is not a fair equivalent, and that even asking for one shows a misunderstanding. That said, a bit of procedural JavaScript can be exited by using an anonymous function. Here&#8217;s some example PHP and then similar code in JavaScript:
</p>
<p><textarea class="php" cols="50" name="code" rows="10"><br />
if ($next_action == &#8216;exit&#8217;) {<br />
	exit;<br />
}</p>
<p>echo &#8216;Made it here!&#8217;;<br />
</textarea></p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
(function() {</p>
<p>	if (nextAction == &#8216;exit&#8217;) {<br />
		return;<br />
	}</p>
<p>	alert(&#8217;Made it here!&#8217;);</p>
<p>})();<br />
</textarea></p>
<p>
While the language behavior is not one-to-one, taking advantage of JavaScript&#8217;s closure already has advantages, among them being able to easily exit a function via <code>return</code>.
</p>
<h3 id="include"><code>include</code> or <code>require</code></h3>
<p>
There have been some arguments about this equivalence in JavaScript. Just like the previous example, this equivalence is not one-to-one and should be thought about in very different terms. That said, here is a quick example of including a PHP file within a PHP script:
</p>
<p><textarea class="php" cols="50" name="code" rows="10"><br />
// Nothing to it, just include the file:<br />
include(&#8217;my_file.php&#8217;);</p>
<p>// This code will only execute after my_file.php has been included:<br />
echo &#8216;File included!&#8217;;<br />
</textarea></p>
<p>
To include an external script in JavaScript, writing a method or including a library is definitely best. <a href="/article/dynamically-load-css-and-js-files/">I wrote about this once</a>, but there are libraries such as <a href="http://developer.yahoo.com/yui/" title="Yahoo! User Interface library">YUI</a> that have <a href="http://developer.yahoo.com/yui/get/">better support with more options</a>. Here&#8217;s what this would look like using YUI:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
// Call YUI&#8217;s method<br />
var objTransaction = YAHOO.util.Get.script(&#8221;http://example.com/my_file.js&#8221;, {<br />
	// This function will only fire after my-file.js has been included:<br />
	onSuccess : function() {<br />
		alert(&#8221;File included!&#8221;);<br />
	}<br />
});<br />
</textarea></p>
<h3 id="redirection">Redirection</h3>
<p>
This one is easy and straightforward. A redirect is accomplished with <a href="http://us3.php.net/manual/en/function.header.php">PHP&#8217;s <code>header()</code> function</a>:
</p>
<p><textarea class="php" cols="50" name="code" rows="10"><br />
header(&#8217;Location: http://example.com&#8217;);<br />
exit; // Not necessary but a good practice<br />
</textarea></p>
<p>
Also very simple in JavaScript:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
location.href = &#8216;http://example.com&#8217;;<br />
</textarea></p>
<h3 id="static-classes">Static classes</h3>
<p>
In PHP, it&#8217;s a pretty common programming pattern to use a static class for encapsulation, sort of namespacing some methods and properties. Here&#8217;s an example:
</p>
<p><textarea class="php" cols="50" name="code" rows="10"><br />
class Tools {</p>
<p>    public static function check_stuff() {<br />
        return true;<br />
    }</p>
<p>}</p>
<p>echo Tools::check_stuff();<br />
</textarea></p>
<p>
For this JavaScript equivalence, it probably makes the most sense to use an object literal:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
Tools = {<br />
	checkStuff : function() {<br />
		return true;<br />
	}<br />
};</p>
<p>alert(Tools.checkStuff());<br />
</textarea></p>
<h3 id="strings">String manipulation</h3>
<p>
String manipulation is absolutely mandatory in most applications. Fortunately, it&#8217;s a breeze thanks to <a href="http://us3.php.net/manual/en/ref.strings.php">PHP&#8217;s fantastic string functions</a>:
</p>
<p><textarea class="php" cols="50" name="code" rows="10"><br />
// Replace portions within a string<br />
echo str_replace(&#8217;Hey&#8217;, &#8216;Hi&#8217;, &#8216;Hey there&#8217;); // outputs &#8220;Hi there&#8221;</p>
<p>// Capitalization<br />
echo strtolower(&#8217;Hey there&#8217;); // outputs &#8220;hey there&#8221;<br />
echo strtoupper(&#8217;Hey there&#8217;); // outputs &#8220;HEY THERE&#8221;</p>
<p>// Get a portion of a string<br />
echo substr(&#8217;Hey there&#8217;, 0, 2); // outputs &#8220;He&#8221;</p>
<p>// Get rid of extraneous whitespace<br />
echo trim(&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hey there&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8217;); // outputs &#8220;Hey there&#8221;<br />
</textarea></p>
<p>
<a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String" title="Mozilla's String reference">Each of these</a> have perfect equivalents except for <code>trim()</code>, which we need to implement ourselves:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
// Replace portions within a string<br />
alert(&#8217;Hey there&#8217;.replace(&#8217;Hey&#8217;, &#8216;Hi&#8217;)); // outputs &#8220;Hi there&#8221;</p>
<p>// Capitalization<br />
alert(&#8217;Hey there&#8217;.toLowerCase()); // outputs &#8220;hey there&#8221;<br />
alert(&#8217;Hey there&#8217;.toUpperCase()); // outputs &#8220;HEY THERE&#8221;</p>
<p>// Get a portion of a string<br />
alert(&#8217;Hey there&#8217;.substr(0, 2)); // outputs &#8220;He&#8221;</p>
<p>// Get rid of extraneous whitespace<br />
trim = function(txt) {<br />
	return txt.replace(/^\s+|\s+$/g, &#8221;);<br />
};</p>
<p>alert(trim(&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hey there&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8217;)); // outputs &#8220;Hey there&#8221;</p>
<p></textarea></p>
<p>
Or, we can augment the <code>String</code> global so that strings will have this method natively:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
String.prototype.trim = function() {<br />
	return this.replace(/^\s+|\s+$/g, &#8221;);<br />
};</p>
<p>// Get rid of extraneous whitespace<br />
alert(&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hey there&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8217;.trim()); // outputs &#8220;Hey there&#8221;<br />
</textarea></p>
<p>
While not all of these equivalents are smooth (or even recommended), it&#8217;s typically helpful when learning a new language to have it compared to a language one is already familiar with. If you can think of any other common PHP tasks that have relatively similar equivalents in JavaScript, please let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/common-php-equivalents-in-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP to Use Backslash as Namespace Separator</title>
		<link>http://scriptnode.com/article/php-to-use-backslash-as-namespace-separator/</link>
		<comments>http://scriptnode.com/article/php-to-use-backslash-as-namespace-separator/#comments</comments>
		<pubDate>Sun, 26 Oct 2008 21:23:52 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Novice]]></category>
		<category><![CDATA[code-style]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=235</guid>
		<description><![CDATA[
After much debate, the minds behind PHP have made an important decision. The good news is, PHP will support namespaces. The (arguably) bad news is, the namespace separator will be the backslash character.


Here&#8217;s a simple example of what this will look like:


$object_instance = new My\PEAR\Module(&#8217;myvar&#8217;);


PHP 5 already already has its criticisms. The developers looked at [...]]]></description>
			<content:encoded><![CDATA[<p>
After <a href="http://wiki.php.net/_media/rfc/php.ns.txt?id=rfc%3Anamespaceseparator&#038;cache=cache">much debate</a>, the minds behind <a href="http://php.net/">PHP</a> have made an important decision. The good news is, PHP will support namespaces. The (arguably) bad news is, <a href="http://news.php.net/php.internals/41374">the namespace separator will be the backslash character</a>.
</p>
<p>
Here&#8217;s a simple example of what this will look like:
</p>
<p><textarea class="php" cols="50" name="code" rows="10"><br />
$object_instance = new My\PEAR\Module(&#8217;myvar&#8217;);<br />
</textarea></p>
<p>
PHP 5 already already has <a href="http://www.tnx.nl/php5.html">its criticisms</a>. The developers looked at <a href="http://wiki.php.net/rfc/namespaceseparator">many different options</a>, including <code>**</code>, <code>^^</code>, <code>%%</code>, <code>:&gt;</code>, <code>:)</code> and <code>:::</code>, but eventually decided on <code>&#92;</code>, which is an odd decision, given that it doesn&#8217;t remotely resemble <a href="http://www.math.tu-berlin.de/polymake/index.html#perl/namespaces.html[3,3,0]">its</a> <a href="http://en.wikipedia.org/wiki/Namespace_identifier">cousins</a>. In fact, it most closely resembles <a href="http://en.wikipedia.org/wiki/MS-DOS">directories in MS-DOS</a> or <a href="http://en.wikipedia.org/wiki/Windows_Registry">other Windows features</a>.
</p>
<p>
The argument was also made that current operators such as  <code>::</code> or <code>-&gt;</code> could have been used, but they <a href="http://us2.php.net/manual/en/language.oop5.paamayim-nekudotayim.php">already had other duties</a>. The argument doesn&#8217;t go far, however, because PHP of course uses the backslash character for escaping.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/php-to-use-backslash-as-namespace-separator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
