<?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>Design Shack &#187; php</title>
	<atom:link href="http://designshack.net/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://designshack.net</link>
	<description>Inspiration, CSS Gallery &#38; Community News</description>
	<lastBuildDate>Thu, 09 Feb 2012 06:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A Designer&#8217;s Introduction to Programming</title>
		<link>http://designshack.net/articles/javascript/an-designers-introduction-to-programming/</link>
		<comments>http://designshack.net/articles/javascript/an-designers-introduction-to-programming/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 19:13:15 +0000</pubDate>
		<dc:creator>Joshua Johnson</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://designshack.net/?p=26023</guid>
		<description><![CDATA[Designers are often quite hesitant to venture beyond the realms of HTML and CSS into the territory of scripting and programming languages, and for good reason. Programming requires a very different set of skills than mocking up web pages. Further, there are so many different things to learn that most designers don&#8217;t even know where [...]]]></description>
			<content:encoded><![CDATA[<p>Designers are often quite hesitant to venture beyond the realms of HTML and CSS into the territory of scripting and programming languages, and for good reason. Programming requires a very different set of skills than mocking up web pages. Further, there are so many different things to learn that most designers don&#8217;t even know where to begin: JavaScript, PHP, Ruby, Rails, how can you possibly keep up? </p>
<p>However, in practice, programming turns out to be much less scary than designers imagine it to be. Today I&#8217;m going to show you how programming is a skill that you can learn once and then apply to each new language that you pick up. We&#8217;ll take a look at three different programming constructs in three different languages to see how similar they really are. If you&#8217;d like to get started in programming but don&#8217;t know a single thing about it, this article is for you.</p>
<p><span id="more-26023"></span><br />
<em>Like the article? Be sure to subscribe to our <a href="feed://feeds.feedburner.com/designshack">RSS feed</a> and follow us on <a href="http://twitter.com/designshack">Twitter</a> to stay up on recent content.</em></p>
<h2>Fear Not, You Just Might Like It</h2>
<p>This site is generally targeted towards designers, and from personal experience I know that approaching programming for the first time as a designer is extremely intimidating. Designers are visual thinkers, we thrive through creation and experimentation using our mind&#8217;s eye and an expert knowledge of the tools of our trade.</p>
<p>Generally, we&#8217;re the kind of people that hated math class and anything else where creativity didn&#8217;t seem to have an obvious foothold.</p>
<div class="pullquote-r">
&#8220;Generally, we&#8217;re the kind of people that hated math class and anything else where creativity didn&#8217;t seem to have an obvious foothold.&#8221;
</div>
<p></ br></p>
<p>Programmers are problem solvers. They love logic puzzles and tinkering with code for hours in the absolute absence of visuals of any kind. Keep in mind that these two categories of people aren&#8217;t mutually exclusive. If you fit both descriptions, you&#8217;ll likely absolutely love programming. </p>
<h3>Not HTML and CSS</h3>
<p>We&#8217;re quickly reaching a point where most web designers know at least a little bit about coding HTML and/or CSS. This experience and knowledge will indeed help you, but perhaps not as much as you might think. </p>
<p>HTML is a *markup language* and CSS is a *style sheet language*, they&#8217;re used primarily to format and insert fairly static content into pre-built pages. How you work with them is in all reality very different from wielding a *scripting language* or *object-oriented programming language*, which are the kind of hardcore programming tools you&#8217;ll find in use on the web today. </p>
<h2>The Big Secret About Programming</h2>
<p>That last part was the bad news, but fear not, there&#8217;s good news on the horizon. If you walk into a bookstore and wander over to the programming section, you&#8217;re likely to be completely intimidated. There are so many different things to learn! This intimidation is furthered when you happen to meet one of the super geniuses that seems to know pretty much all of the programming languages in existence. How in the world do people find the time to learn this stuff? Many beginners are so completely put off at this point that they give up before they ever even try.</p>
<div class="pullquote-r">
&#8220;Programming languages tend to be very similar. Many of them use the same building blocks and similar tool sets to achieve their goals.&#8221;
</div>
<p></ br></p>
<p>The big secret that you don&#8217;t know if you&#8217;ve never really jumped into programming is that programming languages tend to be very similar. Many of them use the same building blocks and similar tool sets to achieve their goals. </p>
<p>I learned this little secret in my first interaction with programming: a Java class in college (not to be confused with JavaScript). Though I myself was a strapping young lad, the class was largely filled with men in their 30s and 40s. Virtually all of them already had jobs in programming and had been at it for twenty years or more. They were simply employed in the use of other languages and attempting to expand their skill set, often at their employer&#8217;s encouragement and expense.</p>
<p>Walking into the class on the first day, none of us knew much, if anything, about Java. However, by the third class, these guys were breezing past me. I was writing horribly simple little code snippets based on the fundamentals that we had learned. These guys were taking the same fundamentals and building elaborate applications with all kinds of cool functions way beyond the requirements for the assignment. </p>
<div class="pullquote-r">
&#8220;Was I inept? It turns out, fortunately for me, that the answer wasn&#8217;t so simple.&#8221;
</div>
<p></ br></p>
<p>Was I inept? It turns out, fortunately for me, that the answer wasn&#8217;t so simple. The reality was that we were learning very different things. I was learning to program through the lens of Java, these guys already knew all about programming and merely needed a little help figuring out how to translate that knowledge into Java. I was a toddler learning to speak for the first time and they were multi-linguals picking up yet another language. </p>
<p>The point of this long story is to illustrate that, after you really get into programming or scripting with one language, the rest becomes much easier because you&#8217;ve dramatically reduced the learning curve. If you know how to code in PHP, the jump to Ruby is much smaller. </p>
<h3>The Building Blocks</h3>
<p>It&#8217;s easy for me to say that programming is built on reusable knowledge, but can I put that into practice? What are these so-called building blocks and how are they similar or different from language to language?</p>
<p>Great question! Let&#8217;s find out by taking a look at some of the most basic but most used programming constructs. Understanding these will jumpstart your programming education regardless of the path or language that you choose to pursue.</p>
<h2>Variables</h2>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.co.uk/wp-content/uploads/prog101-1.jpg" alt="screenshot" width="510"/></div>
<p>Variables are a wonderful thing. If you&#8217;ve ever used <a href="http://lesscss.org/">LESS</a> or something similar then you&#8217;ve seen the wonders of variables and how they can save immense amount of time. I&#8217;m still not sure if they belong in CSS, but the programmer in me sure wants to see them there.</p>
<p>A variable is essentially a way to store information. You can store something as simple as a value, say 39, or something as complex as an entire array of data containing hundreds of values. Think of a variable as a box. Boxes themselves don&#8217;t seem all that important, it&#8217;s what&#8217;s inside that counts. However, boxes do make for a convenient way to store and access certain items. </p>
<h3>JavaScript Variables</h3>
<p>Let&#8217;s jump to an example. Since JavaScript is a likely first candidate for moving past HTML and CSS, we&#8217;ll start all of our examples there. Here&#8217;s how variables work in JavaScript:</p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Creating a JavaScript Variable</span>
<span style="color: #003366; font-weight: bold;">var</span> contentWidth<span style="color: #339933;">;</span>
contentWidth <span style="color: #339933;">=</span> <span style="color: #CC0000;">496</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Using the Variable</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>contentWidth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p></ br></p>
<p>Here you can see exactly how variables are used in JavaScript. First, you &#8220;declare&#8221; a variable. This simply creates it or brings it into existence, when doing this you always want to name it something indicative of its contents. Next, you assign it a value (often these two steps are combined), in our case I threw in an arbitrary number. Remember that, &#8220;contentWidth&#8221; is our box, which now holds a value. Any time we want to access that box, we just use the variable name, so to end the example out I created an alert and inserted the variable. This will pop up a box with the value &#8220;496&#8243;. </p>
<p>This may seem useless here but imagine we had three hundred lines of code that referenced that variable frequently. Any time the value of that variable changes, our code still works perfectly because the variable name has stayed constant. Without variables, this would be impossible and much more code would result. </p>
<h3>PHP Variables</h3>
<p>Now that we have that solid foundation of information, learning variables in other languages becomes much easier. This time we&#8217;re not learning what variables are and how they work, only how to use them in other languages. </p>
<p>Here&#8217;s a look at how variables work in PHP:</p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Creating a PHP Variable</span>
<span style="color: #000088;">$contentWidth</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">496</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Using the Variable</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$contentWidth</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p></ br></p>
<p>There&#8217;s a lot of similarity here. Notice the use of semicolons to denote the end of a statement and how comments use the same syntax. This time we declared our variable and assigned it a value in a single line. We can see that the syntax for this is nearly identical, with the important distinction that PHP uses a &#8220;$&#8221; at the beginning of a variable name. Note that the &#8220;echo&#8221; command merely prints something to the browser page.</p>
<h3>Ruby Variables</h3>
<p>Now let&#8217;s take a look at variables in Ruby. </p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Creating a Ruby Variable</span>
contentWidth = <span style="color:#006666;">496</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Using the Variable</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> contentWidth;</pre></div></div>

</div>
<p></ br></p>
<p>By now you can see that a lot of what&#8217;s involved in learning a new language is just figuring out its various quirks. For instance, JavaScript and PHP used semicolons at the end of a line, Ruby does not. Also, this time around comments are created with  &#8220;#&#8221; instead of &#8220;//&#8221; and we use &#8220;puts&#8221; to output our result. </p>
<h2>Loops</h2>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.co.uk/wp-content/uploads/prog101-2.jpg" alt="screenshot" width="510"/></div>
<p>Another popular programming construct is a loop. With loops you can save yourself the trouble of repeating code again and again by automating the process you want to achieve. When creating a loop, you must be sure to declare the parameters of the loop, such as the conditions for when the loop will cease. You don&#8217;t want the actions continuing on infinitely so you must create a condition whereby the loop will be terminated. </p>
<h3>JavaScript Loops</h3>
<p>In any given language, there are usually multiple types of loops. Here&#8217;s one type in JavaScript, the &#8220;while&#8221; loop. In this example we set up a variable, then create a chunk of code that should execute again and again for as long as the variable is less than 50. Then within that block of code, we define the actions to be repeated. In this case we&#8217;re logging the value of the variable to the console, then increasing the variable&#8217;s value by one. Since the variable is increasing every time the code runs, it will eventually hit 50 and the loop will terminate. </p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// JavaScript &quot;while&quot; Loop</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
	x<span style="color: #339933;">++;</span> <span style="color: #006600; font-style: italic;">// Increases the variable by one</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p></ br></p>
<p>This same thing could be achieved with a &#8220;for&#8221; loop. This version a little more succinct as is states the value of the variable to start, the condition under which the loop terminates and the action to take on the variable all in one line. </p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// JavaScript &quot;for&quot; Loop</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>x<span style="color: #339933;">=</span><span style="color: #CC0000;">25</span><span style="color: #339933;">;</span>x<span style="color: #339933;">&lt;</span><span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p></ br></p>
<h3>PHP Loops</h3>
<p>Once again we find that PHP is very similar to JavaScript. After reading through the examples above, you should be able to effortlessly grasp the PHP equivalent. Here once again we have a while loop and a for loop, each of which contains nearly identical syntax to their JavaScript brethren. </p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// PHP &quot;while&quot; Loop</span>
&nbsp;
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$x</span><span style="color: #339933;">++;</span> <span style="color: #666666; font-style: italic;">// Increases the variable by one</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p></ br></p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// PHP &quot;for&quot; Loop</span>
&nbsp;
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p></ br></p>
<h3>Ruby Loops</h3>
<p>Here are the &#8220;while&#8221; and &#8220;for&#8221; loops in Ruby. As you can begin to see, Ruby is much looser in its syntax than the other two languages we&#8217;re using. Not only are there no semicolons, the curly brackets aren&#8217;t present either. Also notice how different the &#8220;for&#8221; loop looks this time around. </p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">//</span> Ruby <span style="color:#996600;">&quot;while&quot;</span> <span style="color:#CC0066; font-weight:bold;">Loop</span>
&nbsp;
x = <span style="color:#006666;">25</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">while</span> x <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">50</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> x
  x<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span> <span style="color:#008000; font-style:italic;"># Increases the variable by one</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</div>
<p></ br></p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">//</span> Ruby <span style="color:#996600;">&quot;for&quot;</span> <span style="color:#CC0066; font-weight:bold;">Loop</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">for</span> x <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006666;">25</span>..<span style="color:#006666;">50</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> x
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</div>
<p></ br></p>
<h2>Functions</h2>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.co.uk/wp-content/uploads/prog101-3.jpg" alt="screenshot" width="510"/></div>
<p>Functions are yet another programming construct created to save you time by avoiding repeating the same code. When you code an application, it will probably perform anywhere from one to thousands of tasks. Each task represents a chunk of commands. When we toss the commands into a function we can simply call that function instead of writing out that full code. In this way a function is a lot like a variable that performs a task or list of tasks.</p>
<h3>JavaScript Functions</h3>
<p>Let&#8217;s build a super basic function in JavaScript that takes any number passed to it and squares it. We start here with the word &#8220;function&#8221; then follow it up with a variable in parentheses. Then inside the function we perform various actions and return a value.</p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Creating the Function</span>
<span style="color: #003366; font-weight: bold;">function</span> squared<span style="color: #009900;">&#40;</span>someNumber<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> someNumber <span style="color: #339933;">*</span> someNumber<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Using the Function</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>squared<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p></ br></p>
<p>When we use the function, we replace the parenthetical variable with any number we like and the code inside the function will be performed on that value. In the example above, the result returned would be 81.</p>
<h3>PHP Functions</h3>
<p>By now you&#8217;re so familiar with the JavaScript to PHP conversion that you should be able to do it all by yourself. Here&#8217;s the syntax:</p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Creating the Function</span>
<span style="color: #000000; font-weight: bold;">function</span> squared<span style="color: #009900;">&#40;</span><span style="color: #000088;">$someNumber</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$someNumber</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$someNumber</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Using the Function</span>
<span style="color: #b1b100;">echo</span> squared<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p></ br></p>
<h3>Ruby Methods</h3>
<p>Once again, Ruby is quite different. In fact, Ruby actually refers to this particular construct as a method, not a function. However, we can still quickly apply our knowledge of functions to decipher how this works.</p>
<div style="overflow: auto; background-color: #eeeeee;">

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">//</span> Creating the Function
<span style="color:#9966CC; font-weight:bold;">def</span> squared<span style="color:#006600; font-weight:bold;">&#40;</span>someNumber<span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#0000FF; font-weight:bold;">return</span> someNumber <span style="color:#006600; font-weight:bold;">*</span> someNumber
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">//</span> Using the Function
<span style="color:#CC0066; font-weight:bold;">puts</span> squared<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

</div>
<p></ br></p>
<p>As you can see, the Ruby method is performing the same task as the JavaScript function. It allows you to pass a number into the parameters, after which is squares that number and returns the value. </p>
<h2>Easy Right?</h2>
<p>By now you should be able to see the importance of understanding the practice of programming independently of any programming language. There is a shared structure and method of programming that you&#8217;ll find throughout the languages that you learn. Many of the differences are *semantic* and amount to slight syntax differences.</p>
<p>Don&#8217;t take this to mean that programming and scripting languages are all the same, they&#8217;re not. They all have a specific purpose that defines how and when you use them. It&#8217;s a lot like learning Adobe Illustrator and then jumping to InDesign. You&#8217;re dealing with two different things created for two different purposes, however, there&#8217;s enough similarity that an expert knowledge of one will help you learn your way around the other. </p>
<h2>Conclusion</h2>
<p>This discussion was not in any way meant to be exhaustive. You don&#8217;t now possess a complete fundamental knowledge of programming. Instead, you merely learned about programming in a way that hopefully makes it more approachable. Instead of seeing a mass of completely different languages that you have to learn, you should now see a collection of related practices in which the study of one actually expands your understanding of the others.</p>
<p>So where should you start? As I mentioned above, JavaScript is definitely the place for web designers to begin. Its application is immediately apparent, especially when combined with <a href="http://jquery.com/">jQuery</a>, to the HTML and CSS that you already know. From there the leap to PHP is much more manageable, which might lead you to study Ruby so you can master <a href="http://rubyonrails.org/">Ruby on Rails</a>; the possibilities are limitless!</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/javascript/an-designers-introduction-to-programming/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Smart File Type Detection Using PHP</title>
		<link>http://designshack.net/articles/php-articles/smart-file-type-detection-using-php/</link>
		<comments>http://designshack.net/articles/php-articles/smart-file-type-detection-using-php/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 08:59:51 +0000</pubDate>
		<dc:creator>Kishore Nallan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[detection]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.designshack.co.uk/?p=689</guid>
		<description><![CDATA[In most web applications today, there is a need to allow users to upload images, audio and video files. Sometimes, we also need to restrict certain types of files from being uploaded &#8211; an executable file being an obvious example. Security aside, one might also want to prevent users from misusing the upload facility, e.g. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.designshack.net/wp-content/uploads/padlock.jpg" alt="padlock" title="padlock" width="200" height="200" class="right size-full wp-image-722" />In most web applications today, there is a need to allow  users to upload images, audio and video files. Sometimes, we also need to  restrict certain types of files from being uploaded &#8211; an executable file being an obvious example.</p>
<p>Security aside, one might also want to prevent users from misusing  the upload facility, e.g. uploading copyrighted music files illegally and using the service to promote piracy! In this article, we&#8217;ll look into a few ways in which we can achieve this.</p>
<p><span id="more-689"></span></p>
<h2>File  type detection using extension and MIME types</h2>
<p>I am not going to talk about this in too much detail as  after all, this is what we normally do when we want to restrict certain files.  We simply get the MIME type of the file using  <code>$_FILES['myFile']['type']</code> and check if it’s of a  valid type.</p>
<p>Or we might scan the last few characters of the file name  and reject files ending with a certain extension.  Unfortunately, these methods are hardly  sufficient, as one can easily change the extension of a file to bypass this  restriction. Furthermore, MIME type information is given by the browser and most browsers, if not all, determine the mime type based upon the  file&#8217;s extension! Hence MIME types can be pretty easily spoofed too.</p>
<p>Let&#8217;s now explore some others ways which offer better fool-proofness.</p>
<h2>Using Magic Bytes</h2>
<p>The best way to determine the file type is by examining the first few bytes of a file &#8211; referred to as &#8220;magic bytes&#8221;. Magic bytes are essentially signatures that vary in length between 2 to 40 bytes in the file headers, or at the end of a file. There are several hundred types of files, and quite a few of them have several file signatures associated with them. You can see a list of file signatures <a href="http://www.garykessler.net/library/file_sigs.html">over here</a>.</p>
<p>Although inconsistent, this is our best bet in detecting file types reliably. This seemingly difficult task has been made really easy by a PECL extension called <a href="http://pecl.php.net/package/Fileinfo">Fileinfo</a>. As of PHP 5.3, Fileinfo is shipped with the main distribution and is enabled by default, so this is definitely a robust and simple way to detect and impose restrictions on the types of files uploaded.</p>
<p>Let&#8217;s now see how we can detect a file type using Fileinfo:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/path/to/file&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// in PHP 4, we can do:</span>
<span style="color: #000088;">$fhandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">finfo_open</span><span style="color: #009900;">&#40;</span>FILEINFO_MIME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mime_type</span> <span style="color: #339933;">=</span> <span style="color: #990000;">finfo_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fhandle</span><span style="color: #339933;">,</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// e.g. gives &quot;image/jpeg&quot; </span>
&nbsp;
<span style="color: #666666; font-style: italic;">// in PHP 5, we can do:</span>
&nbsp;
<span style="color: #000088;">$file_info</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> finfo<span style="color: #009900;">&#40;</span>FILEINFO_MIME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// object oriented approach!</span>
<span style="color: #000088;">$mime_type</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file_info</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buffer</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// e.g. gives &quot;image/jpeg&quot;</span>
&nbsp;
<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mime_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;image/jpeg&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #666666; font-style: italic;">// your actions go here...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Handling image uploads</h2>
<p>If you intend to allow only image uploads, then you can use  the inbuilt <code>getimagesize()</code> function to ensure that the user is actually uploading a valid image file. This functions returns false, if the file is not a valid image file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//  Let's assume that the name attribute of the file input field you have used is &quot;myfile&quot;</span>
&nbsp;
<span style="color: #000088;">$tempFile</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myFile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// path of the temp file created by PHP during upload</span>
<span style="color: #000088;">$imginfo_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tempFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// returns a false if not a valid image file</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$imginfo_array</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$mime_type</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$imginfo_array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mime'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mime_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
&nbsp;
	<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;image/jpeg&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #666666; font-style: italic;">// your actions go here...</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;This is not a valid image file&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Reading and interpreting magic bytes manually</h2>
<p>If for some reason, you are not able to install Fileinfo, then you can still manually determine the file type by reading the first few bytes of a file and comparing them with known magic bytes associated with the particular file type. This process definitely has an element of trial and error, because there is still a chance that there are a few undocumented magic bytes associated with legitimate file formats. As a result, valid files could be rejected by your system. However it&#8217;s not impossible as a couple of years back, I was asked to work on a script that allowed only genuine mp3 files to be uploaded, and since we could not use Fileinfo, we resorted to this manual scanning. It took me a  while to account for some of the undocumented magic bytes for mp3, but pretty soon, I got a stable upload script running.</p>
<p>Before I end, I would just like to part with a general word of caution: Make sure that you never call an <code>include()</code> with a file that was uploaded, as PHP  code can very well be hidden as part of the picture, and the picture would pass your tests for file validation just fine, only to cause havoc when executed by the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/php-articles/smart-file-type-detection-using-php/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>The Footer Copyright Notice</title>
		<link>http://designshack.net/articles/the-footer-copyright-notice/</link>
		<comments>http://designshack.net/articles/the-footer-copyright-notice/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 11:33:35 +0000</pubDate>
		<dc:creator>David Appleyard</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[automatically. copyright]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[footer]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://localhost:8888/?p=156</guid>
		<description><![CDATA[Almost all websites contain some sort of copyright notice in their footer (e.g. Copyright © 2008 Apple Inc. All rights reserved.). But what&#8217;s the best way to do this? We&#8217;re going to take you through the requirements for your copyright notice, and a nifty JS (or PHP) trick for ensuring that your copyright year is [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.designshack.net/postimages/copyright.jpg" alt="Copyright Footer Notice" class="right" />Almost all websites contain some sort of copyright notice in their footer (e.g. <i>Copyright © 2008 Apple Inc. All rights reserved.</i>). But what&#8217;s the best way to do this? We&#8217;re going to take you through the requirements for your copyright notice, and a nifty JS (or PHP) trick for ensuring that your copyright year is always up to date.</p>
<h2>What&#8217;s required?</h2>
<p>The humble copyright notice is always useful to show in the footer, as a way of stating your claim over a site. Interestingly, however, it is not required for you to have copyright over the graphics, content and artwork of your site. This comes into place as soon as you&#8217;ve created the content and placed it in the public domain. Placing a copyright notice is still advisable to deter potential plagiarists and stake your claim. The generally accepted format is:</p>
<p><i>Copyright &copy; 2008 Design Shack</i></p>
<p>There are a couple of points to make:</p>
<ul>
<li>Make sure that the year is current (see below)</li>
<li>Use the HTML code &amp;copy; to display the copyright symbol, ensuring that your site&#8217;s XHTML is valid</li>
<li>Ensure the word &#8216;copyright&#8217; appears</li>
</ul>
<p>If you have specific requirements for how people can use certain content of your site, using a <a href="http://creativecommons.org/license/">Creative Commons License</a> would be advised. This allows you to select how content can be used in a more official and controlled manner.</p>
<h2>Future proof</h2>
<p>When creating a website, it can be incredibly tempting to simply drop in the basic requirements, and not think too heavily about future proofing your site. One of the most telling signs that a site isn&#8217;t regularly updated is an out of date copyright year. This can be very easily made automatic, through the use of a simple piece of PHP or JS code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="pre" style="font-family:monospace;">&lt;?php echo date(&quot;Y&quot;); ?&gt;</pre></td></tr></table></div>

<p>Alternatively, if you would prefer to use JavaScript, the following works well:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>d.<span style="color: #660066;">getFullYear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Stick with one of these methods, and you&#8217;ll never be kicking yourself in February again for not updating the copyright year!</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/the-footer-copyright-notice/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>An Informative 404 Page</title>
		<link>http://designshack.net/articles/an-informative-404-page/</link>
		<comments>http://designshack.net/articles/an-informative-404-page/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 11:40:14 +0000</pubDate>
		<dc:creator>David Appleyard</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[404]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[reporting]]></category>

		<guid isPermaLink="false">http://localhost:8888/?p=171</guid>
		<description><![CDATA[Classic 404 error pages are prone to being relatively useless. Whilst a well designed page can provide a means to find what they are looking for, wouldn&#8217;t it be great if you could find out more about what went wrong? This tutorial will show you how simple it is to have an explanatory email sent [...]]]></description>
			<content:encoded><![CDATA[<p>Classic 404 error pages are prone to being relatively useless. Whilst a <a href="http://designshack.net/tutorials/custom-404-error-page">well designed page</a> can provide a means to find what they are looking for, wouldn&#8217;t it be great if you could find out more about what went wrong? This tutorial will show you how simple it is to have an explanatory email sent to you whenever a visitor hits a 404 page.</p>
<p><span id="more-171"></span></p>
<h2>Getting Started</h2>
<p>The first thing is to implement a custom 404 error page. We&#8217;ve <a href="http://designshack.net/tutorials/custom-404-error-page">covered this before</a>, but it essentially involves adding the following to your root .htaccess file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">ErrorDocument <span style="color: #CC0000;">404</span> http<span style="color: #339933;">:</span><span style="color: #006600; font-style: italic;">//www.yoursite.com/404.php</span></pre></td></tr></table></div>

<p>This will instruct all 404 error requests to be sent to the specified page rather than showing a default message.</p>
<h2>The PHP Code</h2>
<p>The following code needs to be customized and placed anywhere in your 404 php page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span> 
<span style="color: #000088;">$myemail</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;insert your email address here&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$SERVER_NAME</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$REQUEST_URI</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;404 Page Error&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;
     404 Page Error Report
     Visitor came from: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_REFERER'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;
     Page not found: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;
&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$myemail</span><span style="color: #339933;">,</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span><span style="color: #000088;">$message</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;From: 404errorpage&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will send you an email which will include (if possible) both the page which the user has been referred from, and the page or resource which couldn&#8217;t be found.</p>
<h2>Useful Applications</h2>
<p>Implementing this has a whole host of useful outcomes. However, the first thing to note is that (unless you have previously added these files), a rapid number of emails will arrive in relation to:</p>
<ul>
<li>favicon.ico</li>
<li>robots.txt</li>
</ul>
<p>It may well be worth adding these files in before implementing the page so that you aren&#8217;t inundated with notifications! This technique can be invaluable when designing a new site, to sift out any broken links which may have been overlooked before launch. It is even more useful when modifying an existing site structure, to ensure that current links to your site are not taking new visitors to missing pages. If this is the case, simple redirects can be added using .htaccess:</p>
<p><b>For a file:</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">RewriteRule file1.<span style="color: #660066;">php</span>$ file2.<span style="color: #660066;">php</span> <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

<p><b>For a directory:</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">RewriteRule <span style="color: #339933;">^</span>oldfolder<span style="color: #339933;">/</span>$  newfolder<span style="color: #339933;">/</span> <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

<p>After a while, you&#8217;ll find that the odd 404 error is due to someone mistyping a URL, or having linked to a genuinely non-existent page on your site. It wouldn&#8217;t be advisable to keep this code in place permanently, but for a few days or weeks after making significant changes it provides a fantastic fail-safe.</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/an-informative-404-page/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Object Caching 361/415 objects using disk: basic

Served from: designshack.net @ 2012-02-09 14:19:32 -->
