<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>Dynamic Drive Forums - Blogs</title>
		<link>http://www.dynamicdrive.com/forums/blog.php</link>
		<description>Dynamic Drive help forum</description>
		<language>en</language>
		<lastBuildDate>Thu, 23 May 2013 00:44:31 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>10</ttl>
		<image>
			<url>http://www.dynamicdrive.com/forums/images/misc/rss.jpg</url>
			<title>Dynamic Drive Forums - Blogs</title>
			<link>http://www.dynamicdrive.com/forums/blog.php</link>
		</image>
		<item>
			<title>Automatic IE upgrades</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?277-Automatic-IE-upgrades</link>
			<pubDate>Fri, 10 May 2013 19:55:07 GMT</pubDate>
			<description>A couple of days ago, I noticed that I have IE10 on my computer, although I did not install this latest IE-version myself. Apparently, Microsoft has started automatic upgrades of IE to its latest version(s). 
 
So I wanted to know how my sites looked like with IE10 (I normally use Firefox). They...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">A couple of days ago, I noticed that I have IE10 on my computer, although I did not install this latest IE-version myself. Apparently, Microsoft has started automatic upgrades of IE to its latest version(s).<br />
<br />
So I wanted to know how my sites looked like with IE10 (I normally use Firefox). They looked normal, but the javascript on certain pages produced unwanted results that were not problematic until now (with IE7/8/9). <br />
<br />
At first, I was unable to find out what caused the trouble. Then I remembered having read somewhere that support for conditional comments has been removed in IE10. So I removed the comments and replaced them with (javascript browser) sniffing lines and voilą: everything was fine again.<br />
<br />
Here's the javascript I used (it seems reliable):<br />
<i>if(/*@cc_on!@*/false){} // if IE<br />
if(/*@cc_on!@*/true){} // if not IE<br />
if (ieversion==7){} // if IE7<br />
if (ieversion&gt;7){} // if IE8 and up<br />
if (ieversion&gt;=8) // if IE8 and up</i><br />
etc.<br />
<br />
For the ie-version lines I used the following script:<br />
<i>&lt;script  type=&quot;text/javascript&quot;&gt;<br />
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))<br />
{ //test for MSIE x.x;<br />
var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number<br />
}<br />
&lt;/script&gt;</i><br />
<br />
I hope this may be useful.</blockquote>

]]></content:encoded>
			<dc:creator>molendijk</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?277-Automatic-IE-upgrades</guid>
		</item>
		<item>
			<title><![CDATA[HedgeClipper [part 2]!]]></title>
			<link>http://www.dynamicdrive.com/forums/entry.php?275-HedgeClipper-part-2-!</link>
			<pubDate>Mon, 21 Jan 2013 00:19:54 GMT</pubDate>
			<description><![CDATA[Hey guys, I hope everyone has had a great start to the new year. 
 
If you're just joining us, we're making *hedgeclipper*: a user log-in system.  Last time (http://dynamicdrive.com/forums/entry.php?273), we outlined the basics of how hedgeclipper will work, and wrote a controller function for the...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Hey guys, I hope everyone has had a great start to the new year.<br />
<br />
If you're just joining us, we're making <b>hedgeclipper</b>: a user log-in system.  <a href="http://dynamicdrive.com/forums/entry.php?273" target="_blank">Last time</a>, we outlined the basics of how hedgeclipper will work, and wrote a controller function for the whole thing.  This time, we're going to focus on <i>checking</i> if the user is logged in or not.  Before we start demanding a username and password, we're going to check if our user already logged in on a previous page visit.  Have a look:<br />
<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;this&nbsp;function&nbsp;checks&nbsp;if&nbsp;a&nbsp;user&nbsp;is&nbsp;currently&nbsp;logged&nbsp;in.<br />&nbsp;*&nbsp;&nbsp;if&nbsp;so,&nbsp;it&nbsp;sets&nbsp;up&nbsp;the&nbsp;user's&nbsp;info&nbsp;for&nbsp;hedgeclipper&nbsp;to&nbsp;use.<br />&nbsp;*&nbsp;<br />&nbsp;*&nbsp;@return&nbsp;bool&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;true&nbsp;if&nbsp;the&nbsp;user&nbsp;is&nbsp;logged&nbsp;in;&nbsp;false&nbsp;otherwise<br />&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">hedgeclipper_checkLoggedIn</span><span style="color: #007700">(){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;hedgeclipper&nbsp;info&nbsp;is&nbsp;stored&nbsp;in&nbsp;session,&nbsp;so&nbsp;make&nbsp;sure&nbsp;one&nbsp;has&nbsp;been&nbsp;started.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;!</span><span style="color: #0000BB">session_id</span><span style="color: #007700">()&nbsp;){&nbsp;</span><span style="color: #0000BB">session_start</span><span style="color: #007700">();&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;check&nbsp;if&nbsp;there&nbsp;are&nbsp;any&nbsp;hooks&nbsp;to&nbsp;do<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_doHook</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'checkLoggedIn_start'&nbsp;</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;check&nbsp;if&nbsp;hedgeclipper&nbsp;info&nbsp;exists:<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;!empty(&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'hedgeclipper'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;pull&nbsp;the&nbsp;data&nbsp;from&nbsp;the&nbsp;session;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;hedgeclipper_userinfo()&nbsp;validates&nbsp;and&nbsp;stores&nbsp;it<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">hedgeclipper_userinfo</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'hedgeclipper'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;now&nbsp;we&nbsp;can&nbsp;check&nbsp;if&nbsp;the&nbsp;user&nbsp;has&nbsp;logged&nbsp;in&nbsp;or&nbsp;not:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #FF8000">#&nbsp;start&nbsp;with&nbsp;a&nbsp;basic&nbsp;login&nbsp;check<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_userinfo</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'auth'</span><span style="color: #007700">,</span><span style="color: #DD0000">'last_login'&nbsp;</span><span style="color: #007700">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">0&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;and&nbsp;see&nbsp;if&nbsp;there's&nbsp;a&nbsp;hook&nbsp;also<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_doHook</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'checkLoggedIn_success'&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;all&nbsp;good.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;if&nbsp;we&nbsp;get&nbsp;to&nbsp;this&nbsp;point,&nbsp;either&nbsp;the&nbsp;user&nbsp;is&nbsp;not&nbsp;logged&nbsp;in,&nbsp;or&nbsp;something&nbsp;went&nbsp;wrong.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_doHook</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'checkLoggedIn_fail'&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>Hedgeclipper stores user info in the $_SESSION superglobal, so basically, this function just checks if there is any user login info present.  <code style="background-color: #FFFFBB">last_login</code> contains a unix timestamp, set when the user last logged in - if it's <code style="background-color: #FFFFBB">0</code>, the user hasn't logged in yet.  If it's greater than 0, then we have a record of the user logging in, and we don't need to ask again.<br />
<br />
There are several spots in this function where we call another function called <a href="http://custom-anything.com/sand/hc/dohook.html" target="_blank"><code style="background-color: #FFFFBB">hedgeclipper_doHook()</code></a>.   This is fun: if there is a function in the hedgeclipper options tied to a particular event, this will run that function and return the result.  This allows us to add custom functionality without rewriting (and possibly breaking!) the actual hedgeclipper functions.<br />
<br />
For example, we might want hedgeclipper to check not only if the user logged in, but that they logged in recently.  No problem!<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:180px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">#&nbsp;EXAMPLE&nbsp;USAGE&nbsp;-&nbsp;not&nbsp;part&nbsp;of&nbsp;hedgeclipper&nbsp;#<br />//&nbsp;write&nbsp;a&nbsp;custom&nbsp;check&nbsp;function:<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">check_recentLogin</span><span style="color: #007700">(){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;check&nbsp;if&nbsp;login&nbsp;was&nbsp;within&nbsp;last&nbsp;hour<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">hedgeclipper_userinfo</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'auth'</span><span style="color: #007700">,</span><span style="color: #DD0000">'last_login'&nbsp;</span><span style="color: #007700">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">()&nbsp;+&nbsp;</span><span style="color: #0000BB">60</span><span style="color: #007700">*</span><span style="color: #0000BB">60&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;not&nbsp;recent...&nbsp;&nbsp;:(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;register&nbsp;our&nbsp;"hook"&nbsp;in&nbsp;options,&nbsp;under&nbsp;the&nbsp;"checkLoggedIn_success"&nbsp;event:<br /></span><span style="color: #0000BB">hedgeclipper_options</span><span style="color: #007700">(&nbsp;array(&nbsp;</span><span style="color: #DD0000">'hooks'</span><span style="color: #007700">=&gt;array(&nbsp;</span><span style="color: #DD0000">'checkLoggedIn_success'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'check_recentLogin'&nbsp;</span><span style="color: #007700">)&nbsp;)&nbsp;);&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>Now, if the user logged in more than an hour ago, this function will return FALSE and the login check will fail.<br />
<br />
Another function that both these functions call is <code style="background-color: #FFFFBB">hedgeclipper_userinfo()</code>.  This one work similarly to the <code style="background-color: #FFFFBB">hedgeclipper_options()</code> function, except that it doesn't manage options: it manages information about the user.<br />
<br />
There are three categories of information to manage:<ul><li style=""><i>auth</i>.  Info about authentication (login status) and authorization (permissions).</li><li style=""><i>user</i>.  Info about the user specifically (e.g., their name, email, etc.).</li><li style=""><i>app</i>.  Arbitrary info that's not specifically about the user or their authorization, but you want to associate with the user and their session.  User preferences might be a good example of info to store here.</li></ul><div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;this&nbsp;function&nbsp;organizes&nbsp;and&nbsp;stores&nbsp;all&nbsp;the&nbsp;info&nbsp;hedgeclipper&nbsp;needs.<br />&nbsp;*<br />&nbsp;*&nbsp;@param&nbsp;string&nbsp;$mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;determines&nbsp;category&nbsp;to&nbsp;use:&nbsp;"auth","user","app"<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;also&nbsp;accepts&nbsp;"all"&nbsp;to&nbsp;get&nbsp;the&nbsp;whole&nbsp;$userinfo&nbsp;array.<br />&nbsp;*&nbsp;@param&nbsp;string|array&nbsp;$info&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;*get*&nbsp;info,&nbsp;pass&nbsp;the&nbsp;key&nbsp;name<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;*set*&nbsp;info,&nbsp;pass&nbsp;an&nbsp;array&nbsp;with&nbsp;"key"=&gt;"new&nbsp;info"<br />&nbsp;*&nbsp;@return&nbsp;string|bool&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;info&nbsp;if&nbsp;requested,&nbsp;or&nbsp;TRUE&nbsp;if&nbsp;new&nbsp;info&nbsp;was&nbsp;set;<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FALSE&nbsp;otherwise<br />&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">hedgeclipper_userinfo</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$mode</span><span style="color: #007700">,</span><span style="color: #0000BB">$info</span><span style="color: #007700">=</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;static&nbsp;containers&nbsp;for&nbsp;each&nbsp;userinfo&nbsp;category<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">static&nbsp;</span><span style="color: #0000BB">$auth</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;</span><span style="color: #0000BB">$app</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;set&nbsp;defaults&nbsp;for&nbsp;each&nbsp;userinfo&nbsp;category<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;empty(&nbsp;</span><span style="color: #0000BB">$auth&nbsp;</span><span style="color: #007700">)&nbsp;){&nbsp;</span><span style="color: #0000BB">$auth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">hedgeclipper_userinfo_auth_default</span><span style="color: #007700">();&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;empty(&nbsp;</span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">)&nbsp;){&nbsp;</span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">hedgeclipper_userinfo_user_default</span><span style="color: #007700">();&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;empty(&nbsp;</span><span style="color: #0000BB">$app&nbsp;&nbsp;</span><span style="color: #007700">)&nbsp;){&nbsp;</span><span style="color: #0000BB">$app&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">hedgeclipper_userinfo_app_default</span><span style="color: #007700">();&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;check&nbsp;$mode:<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">switch(&nbsp;</span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;use&nbsp;$auth&nbsp;array:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">case&nbsp;</span><span style="color: #DD0000">'auth'</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;if&nbsp;$info&nbsp;is&nbsp;the&nbsp;name&nbsp;of&nbsp;an&nbsp;$auth&nbsp;key,&nbsp;return&nbsp;that&nbsp;item's&nbsp;value.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$auth</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$auth</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;if&nbsp;$auth&nbsp;contains&nbsp;the&nbsp;name&nbsp;of&nbsp;an&nbsp;$auth&nbsp;key&nbsp;and&nbsp;a&nbsp;value,&nbsp;set&nbsp;the&nbsp;key&nbsp;to&nbsp;that&nbsp;value.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$auth</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">0</span><span style="color: #007700">&#93;&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$auth</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">0</span><span style="color: #007700">&#93;&#93;&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">1</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;otherwise,&nbsp;return&nbsp;false.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;use&nbsp;$user&nbsp;array:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">case&nbsp;</span><span style="color: #DD0000">'user'</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;works&nbsp;the&nbsp;same&nbsp;way&nbsp;as&nbsp;"auth".<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">0</span><span style="color: #007700">&#93;&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">0</span><span style="color: #007700">&#93;&#93;&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">1</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;use&nbsp;$app:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">case&nbsp;</span><span style="color: #DD0000">'app'</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;works&nbsp;the&nbsp;same&nbsp;way,&nbsp;but&nbsp;allows&nbsp;new&nbsp;keys&nbsp;to&nbsp;be&nbsp;set&nbsp;arbitrarily.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$app</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#93;&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$app</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$app</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">0</span><span style="color: #007700">&#93;&#93;&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">1</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;use&nbsp;all:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">case&nbsp;</span><span style="color: #DD0000">'all'</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;set&nbsp;all&nbsp;new&nbsp;userinfo<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;isset(&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'auth'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;&amp;&amp;&nbsp;isset(&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'user'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;&amp;&amp;&nbsp;isset(&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'app'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$auth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'auth'</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'user'</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$app&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'app'</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array(&nbsp;</span><span style="color: #0000BB">$auth</span><span style="color: #007700">,</span><span style="color: #0000BB">$user</span><span style="color: #007700">,</span><span style="color: #0000BB">$app&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;do&nbsp;nothing:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">default:&nbsp;return;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>There are a few functions that this function uses to set up default values for &quot;auth&quot;, &quot;user&quot;, and &quot;app&quot;.  This is also where you can &quot;hook&quot; your own functions, if you want to add to the default items.  Because this post is too long already, I'll <a href="http://custom-anything.com/sand/hc/userinfo_defaults.html" target="_blank">link to those functions</a> so you can check 'em out.<br />
<br />
The last bit of code we'll look at today is hedgeclipper_checkLoggedIn()'s complementary function: where checkLoggedIn <i>gets</i> info from the session, <code style="background-color: #FFFFBB">hedgeclipper_rememberLoggedIn()</code> will <i>store</i> info in the session.<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:156px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;this&nbsp;function&nbsp;updates&nbsp;the&nbsp;php&nbsp;session&nbsp;with&nbsp;hedgeclipper's&nbsp;info.<br />&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">hedgeclipper_rememberLoggedIn</span><span style="color: #007700">(){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;update&nbsp;the&nbsp;"last&nbsp;activity"&nbsp;time<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_userinfo</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'auth'</span><span style="color: #007700">,array(&nbsp;</span><span style="color: #DD0000">'last_activity'</span><span style="color: #007700">,</span><span style="color: #DD0000">'now'&nbsp;</span><span style="color: #007700">)&nbsp;);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;save&nbsp;all&nbsp;hedgeclipper&nbsp;userinfo&nbsp;to&nbsp;session<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;!</span><span style="color: #0000BB">session_id</span><span style="color: #007700">()&nbsp;){&nbsp;</span><span style="color: #0000BB">session_start</span><span style="color: #007700">();&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'hedgeclipper'</span><span style="color: #007700">&#93;&nbsp;=&nbsp;</span><span style="color: #0000BB">hedgeclipper_userinfo</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'all'&nbsp;</span><span style="color: #007700">);<br />}&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>Let's take a quick break from coding and look at some good ol'fashioned HTML.  We're going to put our HTML in &quot;template&quot; files - that way, we can keep it organized and we can also edit or replace individual parts easily.<br />
<b>hedgeclippertemplate.loginform.html</b><div class="bbcode_container">
	<div class="bbcode_description">HTML Code:</div>
	<pre class="bbcode_code" style="height:18*12px};"><i><span style="color:#000080">&lt;!--------------------------------------------
html markup for hedgeclipper login form.
    there are two &quot;placeholders&quot; in the markup.
    the hedgeclipper functions will look for and replace them with dynamic values:
    -- %action% will be replaced by the appropriate URL to submit the form to.
    -- %token% will be replaced by a unique security token to help prevent brute force attacks.
--------------------------------------------&gt;</span></i>
<span style="color:#FF8000">&lt;form class=<span style="color:#0000FF">&quot;hedgeclipper_login&quot;</span> action=<span style="color:#0000FF">&quot;%action%&quot;</span> method=<span style="color:#0000FF">&quot;post&quot;</span>&gt;</span>
    <span style="color:#FF8000">&lt;fieldset&gt;</span>
        <span style="color:#FF8000">&lt;legend&gt;</span>log in with <span style="color:#000080">&lt;b&gt;</span>hedgeclipper<span style="color:#000080">&lt;/b&gt;</span><span style="color:#FF8000">&lt;/legend&gt;</span>
        <span style="color:#FF8000">&lt;input type=<span style="color:#0000FF">&quot;hidden&quot;</span> name=<span style="color:#0000FF">&quot;hedgeclipper&#91;token&#93;&quot;</span> value=<span style="color:#0000FF">&quot;%token%&quot;</span>&gt;</span>
        <span style="color:#000080">&lt;p&gt;</span><span style="color:#FF8000">&lt;label for=<span style="color:#0000FF">&quot;hedgeclipper&#91;email&#93;&quot;</span>&gt;</span>Email: <span style="color:#FF8000">&lt;/label&gt;</span><span style="color:#FF8000">&lt;input type=<span style="color:#0000FF">&quot;email&quot;</span> name=<span style="color:#0000FF">&quot;hedgeclipper&#91;email&#93;&quot;</span> required&gt;</span>
        <span style="color:#000080">&lt;p&gt;</span><span style="color:#FF8000">&lt;label for=<span style="color:#0000FF">&quot;hedgeclipper&#91;password&#93;&quot;</span>&gt;</span>Password: <span style="color:#FF8000">&lt;/label&gt;</span><span style="color:#FF8000">&lt;input type=<span style="color:#0000FF">&quot;password&quot;</span> name=<span style="color:#0000FF">&quot;hedgeclipper&#91;password&#93;&quot;</span> required&gt;</span>
        <span style="color:#000080">&lt;p&gt;</span><span style="color:#FF8000">&lt;label&gt;</span><b><i>&amp;nbsp;</i></b><span style="color:#FF8000">&lt;/label&gt;</span><span style="color:#FF8000">&lt;input type=<span style="color:#0000FF">&quot;submit&quot;</span> value=<span style="color:#0000FF">&quot;Log In&quot;</span>&gt;</span>
    <span style="color:#FF8000">&lt;/fieldset&gt;</span>
<span style="color:#FF8000">&lt;/form&gt;</span></pre>
</div>In your browser, the form will look something like this:<br />
<img src="http://custom-anything.com/sand/imgs/hc_login_unstyled.png" border="0" alt="" /><br />
You might clean it up with some basic CSS:<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:48px;">.hedgeclipper_login{ border: 1px outset gray; background: #eee; font-family: sans-serif; }
.hedgeclipper_login label{ display: inline-block; width: 8em; }</pre>
</div><img src="http://custom-anything.com/sand/imgs/hc_login_styled.png" border="0" alt="" /><br />
<br />
That's a lot of stuff we covered...!  Next time, we'll look how to handle actual login attempts, and also some database stuff!<br />
<br />
See ya next time,<br />
<br />
-Adrian</blockquote>

]]></content:encoded>
			<dc:creator>traq</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?275-HedgeClipper-part-2-!</guid>
		</item>
		<item>
			<title>Things we can do with location.search</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?274-Things-we-can-do-with-location-search</link>
			<pubDate>Tue, 01 Jan 2013 22:42:50 GMT</pubDate>
			<description><![CDATA[The javascript location.search propery sets / returns the query portion of a URL, including the question mark (?). This means that if we put <a onclick="location.search='this is a test'">test</a> in a file named our_file.html, the URL (our_file.html) will be replaced with our_file.html?this is a...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">The javascript <font color="#FF0000">location.search</font> propery sets / returns the query portion of a URL, including the question mark (?). This means that if we put <i>&lt;a onclick=&quot;<font color="#FF0000">location.search</font>='this is a test'&quot;&gt;test&lt;/a&gt;</i> in a file named <i>our_file.html</i>, the URL (<i>our_file.html</i>) will be replaced with <i>our_file.html?this is a test</i> after a click on the link. Having arrived at <i>our_file.html?this is a test</i>, we can go back to <i>our_file.html</i> by using the browser's history (back) button. The content of <i>our_file.html?this is a test</i> will be identical to the content of <i>our_file.html</i>, unless we do something about it. And we should, because having two identical pages on a site is not a very useful thing.<br />
<br />
We can use the search portion of a URL containing a question mark - where the search portion is the string to the right of '?' - to pass data from one page (<i>our_file.html</i>) to another (<i>our_file.html?this is a test</i>). Here's a script that we could use for that purpose. It must be put immediately before the closing body tag of the page (here: <i><i>our_file.html</i></i>)<br />
&lt;script&gt;<br />
var data=<font color="#FF0000">location.search</font>;<br />
if(data)<br />
{<br />
data = <font color="#FF0000">location.search</font>.substring(1); // needed to remove the '?'<br />
//do something with the data<br />
data=''<br />
}<br />
&lt;/script&gt;<br />
<br />
In the above script, we can replace <i>//do something with the data</i> with some concrete javascript line using the variable<i> data</i>, like <i>document.write(data)</i> - or, if we want the data to be readable: <i>document.write(unescape(data))</i>. The result we be that a text string (<i>this is a test</i>) is passed from <i>our_file.html</i> to <i>our_file.html?this is a test</i> when we click on our link in <i>our_file.html</i> (where the link is provided by <i>&lt;a onclick=&quot;<font color="#FF0000">location.search</font>='this is a test'&quot;&gt;test&lt;/a&gt;</i>, see above).<br />
<br />
A more interesting example of passing data from one page to another using the search portion of a URL is the following, which assumes that (i) we have an iframe in <i>our_file.html: &lt;iframe name=<b>&quot;ifr</b>&quot; style=&quot;position: absolute; width: 300px; height: 300px; background: white&quot; src=&quot;some_page.html&quot;&gt;&lt;/iframe&gt;</i> and that (ii) <i>//do something with the data</i> in the script above is replaced with <i><b>ifr</b>.location.replace(data)</i>. If we now replace our original link in <i>our_file.html</i> with <i>&lt;a onclick=&quot;<font color="#FF0000"><font color="#FF0000">location.search</font></font>='some_other_page.html'&quot;&gt;some other page&lt;/a&gt;</i>, a click on the new link will take us to a new page <i>our_file.html?some_other_page.html</i> which is identical to <i>our_file.html</i> except for the content of the iframe, which loads <i>some_other_page.html</i> (in <i>our_file.html?some_other_page.html</i>). Of course, in order for this to work, we must create two pages <i>some_page.html</i> and <i>some_other_page.html</i> first.<br />
<br />
This demonstrates that <font color="#FF0000">location.search</font> can be used to (i) preserve the content of a given page (here: <i>our_file.html</i>) and, at the same time, to (ii) change the content of an iframe contained in it while going from one page to another page (here: <i>our_file.html?some_other_page.html</i>). So a navigation menu in the 'main page' (here: <i>our_file.html</i>) will be visible on all pages of a site constructed along the lines given here.<br />
IMPORTANT: <i>&lt;a onclick=&quot;<font color="#FF0000">location.search</font>='some_other_page.html'&quot;&gt;some other page&lt;/a&gt;</i> was used above in a non-iframed page to go from <i>some_page.html</i> (in an iframe) to <i>some_other_page.html</i> (in an iframe). If we want to change the iframe's content <b>from within</b> the iframe, we should do &lt;<i>a onclick=&quot;<b>parent</b>.<font color="#FF0000">location.search</font>='some_other_page.html'&quot;&gt;some other page&lt;/a&gt;</i>. <br />
<br />
Demo and explanations <a href="http://mesdomaines.nu/eendracht/include_menu_web_templates6h" target="_blank"><i><u>here</u></i></a>. The demo contains features that have nothing to do with <font color="#FF0000">location.search</font>, because I already used the files of the demo for another purpose. But I think it explains rather what I want to show.<br />
<br />
Arie.</blockquote>

]]></content:encoded>
			<dc:creator>molendijk</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?274-Things-we-can-do-with-location-search</guid>
		</item>
		<item>
			<title><![CDATA[A Really Big PHP Tutorial [...part 1]]]></title>
			<link>http://www.dynamicdrive.com/forums/entry.php?273-A-Really-Big-PHP-Tutorial-part-1</link>
			<pubDate>Mon, 17 Dec 2012 03:04:07 GMT</pubDate>
			<description><![CDATA[. 
Who Is This For? 
 
I'm going to make some assumptions about you, as a programmer: 
 
* You're probably not an expert with PHP, but you know at least "the basics." 
If you're a copy+paste PHP coder, that's okay, but hopefully you won't be once we're done. 
* You have some *patience*.  Yes, you...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">.<br />
<font size="3"><i>Who Is This For?</i></font><br />
<br />
I'm going to make some assumptions about you, as a programmer:<br />
<ul><li style="">You're probably not an expert with PHP, but you know at least &quot;the basics.&quot;<br />
If you're a copy+paste PHP coder, that's okay, but hopefully you <i>won't be</i> once we're done.</li><li style="">You have some <b>patience</b>.  Yes, you want to see results; but you can trust that <i>they're coming</i>.</li><li style="">You're &quot;okay&quot; with doing things you've never tried before.</li></ul><br />
There <i>are</i> some <a href="http://dynamicdrive.com/forums/entry.php?269" target="_blank">things I would like you to know</a> before we get started.<br />
<br />
While I assume that you know your way around a basic PHP script, I want to assure you that if you're a newbie, nothing in here will break you!  This tutorial is more about &quot;the plan,&quot; and <i>how</i> to write scripts, and less about understanding all those complicated, obscure, hyper-abstracted details <a href="http://www.dynamicdrive.com/forums/entry.php?270" target="_blank">that I usually blather on about</a>.<br />
<br />
It <i>is</i> a long endeavor - my guess is four to six posts.  That &quot;patience&quot; thing I mentioned above will pay off.  There are going to be lots of little things that we set up in one part of the tutorial, but don't get around to using until much later.<br />
<br />
<br />
<font size="3"><i>What Are We Doing?</i></font><br />
<br />
We're going to build something.  We're going to create a user log-in system.  <br />
<br />
<div style="margin-left:40px"><font size="3">I shall call it <b><a href="http://xkcd.com/119/" target="_blank">hedgeclipper</a></b>.</font><br />
<img src="http://custom-anything.com/images/hedgeclipper_icon.png" border="0" alt="" /></div><br />
I've mentioned before that, when you write a program, it's a good idea to write it <a href="http://www.dynamicdrive.com/forums/entry.php?267" target="_blank">PHP-first</a>.  So we're going to do that.  A neat side-effect of PHP-first is that it forces you to <b>plan out your application</b> - since you're not just writing code &quot;as you think of it,&quot; you need to know what you're going to write <i>in advance</i>.<br />
<br />
So let's make some goals for <u>hedgeclipper</u>.  First, the obvious:<ul><li style="">Requires the person trying to access the page/program to be logged in.</li><li style="">Allows the person to log in/out.</li><li style="">Is secure<font color="#0000CD">*<br />
*</font> <font color="#808080"><i>What &quot;secure&quot; implies is deserving of <a href="http://www.openwall.com/articles/PHP-Users-Passwords" target="_blank">its own discussion</a>, but let's not interrupt our &quot;flow&quot; right now.</i></font></li></ul><br />
Some not-so-obvious goals:<br />
<ul><li style="">Flexible: Can be configured and used in different ways on a page-specific basis.</li><li style="">Extensible: Code can be changed easily; adding, removing, or switching features is not difficult.</li><li style="">Portable: Can be used on new <i>or</i> existing pages, on different sites or servers, or with different codebases.</li></ul><br />
And a &quot;Feature List&quot;:<br />
<ul><li style="">verify username + password</li><li style="">track logged-in status (so we don't have to login on every request)</li><li style="">control access to restricted pages</li><li style="">control access to restricted content on pages</li><li style="">allow user to log out</li></ul><br />
<br />
Sounds simple, right?  :p<br />
<br />
<font color="#808080"><i>Notice that, to start, we're not going to worry about some common features, like registration pages, email validation, password recovery, or permissions and groups.  That doesn't mean we <b>won't</b>: we're just going to start with the bare minimum.  But we're going to make the code flexible enough that we can add these features (and more!) later on, without making a huge mess in the process.<br />
<br />
We <b>are</b> going to make control panels (how else could we set up user accounts/passwords?).  Those will come later, on a separate page.</i></font><br />
<br />
Next, describe the situations <u>hedgeclipper</u> might encounter, and what it should do in those situations.<br />
<br />
<div style="margin-left:40px">...What if we need to configure options for the program (hint: we do)?<br />
<div style="margin-left:40px">Do it!</div>...What if we need to find out if the user is logged in or not (hint: we do)?<br />
<div style="margin-left:40px">...What if the user already logged in?<br />
 <div style="margin-left:40px">Cool.  I'll remember that.</div>...What if the user is logging in <i>right now</i>?<br />
 <div style="margin-left:40px">...What if the login attempt is valid?<br />
  <div style="margin-left:40px">Cool.  I'll remember that.</div>...What if it's no good?<br />
  <div style="margin-left:40px">Reject the login attempt.</div></div>...What if the user is logging <i>out</i>?<br />
 <div style="margin-left:40px">Cool.  I'll remember that.</div>...What if the user is not logged in, and only logged-in users are allowed to continue?<br />
  <div style="margin-left:40px">Require user to log in.</div></div></div><br />
<br />
<font size="3"><i>Guess What?</i></font><br />
<br />
We just wrote <u>hedgeclipper</u>'s main controller script.<br />
<br />
<u>hedgeclipper_controller()</u><div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;this&nbsp;function&nbsp;allows&nbsp;_hedgeclipper_&nbsp;to&nbsp;decide&nbsp;what&nbsp;it&nbsp;should&nbsp;do.<br />&nbsp;*<br />&nbsp;*&nbsp;@param&nbsp;array|null&nbsp;$options&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;configuration&nbsp;options.&nbsp;we'll&nbsp;talk&nbsp;a&nbsp;little&nbsp;about&nbsp;this&nbsp;next.<br />&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">hedgeclipper_controller</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">=</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">){<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;What&nbsp;if&nbsp;we&nbsp;need&nbsp;to&nbsp;configure&nbsp;options&nbsp;for&nbsp;the&nbsp;program&nbsp;(hint:&nbsp;we&nbsp;do)?<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;Do&nbsp;it!<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipperOptions</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;What&nbsp;if&nbsp;we&nbsp;need&nbsp;to&nbsp;find&nbsp;out&nbsp;if&nbsp;the&nbsp;user&nbsp;is&nbsp;logged&nbsp;in&nbsp;or&nbsp;not&nbsp;(hint:&nbsp;we&nbsp;do)?<br />&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;What&nbsp;if&nbsp;the&nbsp;user&nbsp;already&nbsp;logged&nbsp;in?<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">hedgeclipper_checkLoggedIn</span><span style="color: #007700">()&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;Cool.&nbsp;&nbsp;I'll&nbsp;remember&nbsp;that.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_rememberLoggedIn</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;What&nbsp;if&nbsp;the&nbsp;user&nbsp;is&nbsp;logging&nbsp;in&nbsp;*right&nbsp;now*?<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}elseif(&nbsp;</span><span style="color: #0000BB">hedgeclipper_isLoggingIn</span><span style="color: #007700">()&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;What&nbsp;if&nbsp;the&nbsp;login&nbsp;attempt&nbsp;is&nbsp;valid?<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">hedgeclipper_validateLogin</span><span style="color: #007700">()&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;Cool.&nbsp;&nbsp;I'll&nbsp;remember&nbsp;that.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_rememberLoggedIn</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;What&nbsp;if&nbsp;it's&nbsp;no&nbsp;good?<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;Reject&nbsp;the&nbsp;login&nbsp;attempt.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_forgetLoggedIn</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_loginFailed</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;What&nbsp;if&nbsp;the&nbsp;user&nbsp;is&nbsp;logging&nbsp;out?<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">hedgeclipper_isLoggingOut</span><span style="color: #007700">()&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;Cool.&nbsp;&nbsp;I'll&nbsp;remember&nbsp;that.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_forgetLoggedIn</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;What&nbsp;if&nbsp;the&nbsp;user&nbsp;is&nbsp;not&nbsp;logged&nbsp;in,&nbsp;and&nbsp;only&nbsp;logged-in&nbsp;users&nbsp;are&nbsp;allowed&nbsp;to&nbsp;continue?<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">hedgeclipperOptions</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'restrict'&nbsp;</span><span style="color: #007700">)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;Require&nbsp;user&nbsp;to&nbsp;log&nbsp;in.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">hedgeclipper_requireLogin</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div><b>Hard part's over.  : )</b><br />
<br />
Now, we just need to write out what all those functions do.  The first thing we'll look at is how we handle setting and checking configuration options.<br />
<br />
<u>hedgeclipperOptions()</u><br />
<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;this&nbsp;function&nbsp;keeps&nbsp;track&nbsp;of&nbsp;hedgeclipper's&nbsp;configuration&nbsp;settings.<br />&nbsp;*&nbsp;&nbsp;it&nbsp;also&nbsp;handles&nbsp;setting&nbsp;custom&nbsp;options,&nbsp;and&nbsp;returns&nbsp;option&nbsp;values&nbsp;when&nbsp;requested.<br />&nbsp;*<br />&nbsp;*&nbsp;@param&nbsp;string|array&nbsp;$opt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;$opt&nbsp;is&nbsp;a&nbsp;string,&nbsp;we're&nbsp;asking&nbsp;for&nbsp;the&nbsp;value&nbsp;of&nbsp;the&nbsp;matching&nbsp;config&nbsp;setting.<br />&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;$opt&nbsp;is&nbsp;an&nbsp;array,&nbsp;we're&nbsp;setting&nbsp;config&nbsp;options&nbsp;with&nbsp;specific&nbsp;values.<br />&nbsp;*&nbsp;@return&nbsp;mixed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returns&nbsp;the&nbsp;value&nbsp;of&nbsp;$opt&nbsp;if&nbsp;$opt&nbsp;is&nbsp;a&nbsp;config&nbsp;option;&nbsp;FALSE&nbsp;otherwise.<br />&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">hedgeclipperOptions</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$opt</span><span style="color: #007700">=</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;more&nbsp;about&nbsp;`static`&nbsp;in&nbsp;the&nbsp;paragraph&nbsp;below<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">static&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;assign&nbsp;default&nbsp;values&nbsp;to&nbsp;configuration&nbsp;options&nbsp;if&nbsp;none&nbsp;exist<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;empty(&nbsp;</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'datatype'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'mysqli'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'datasource'&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">null<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'deny'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">''<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'expiry'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">null<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'hooks'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,</span><span style="color: #DD0000">'restrict'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;if&nbsp;$opt&nbsp;is&nbsp;an&nbsp;array,&nbsp;use&nbsp;it&nbsp;to&nbsp;update&nbsp;$options<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$opt&nbsp;</span><span style="color: #007700">)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_merge</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">,</span><span style="color: #0000BB">$opt&nbsp;</span><span style="color: #007700">);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;if&nbsp;$opt&nbsp;is&nbsp;a&nbsp;string,&nbsp;and&nbsp;a&nbsp;matching&nbsp;setting&nbsp;exists&nbsp;in&nbsp;$options,&nbsp;return&nbsp;its&nbsp;value<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$opt&nbsp;</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;isset(&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$opt</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">&#91;</span><span style="color: #0000BB">$opt</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;if&nbsp;we&nbsp;get&nbsp;to&nbsp;this&nbsp;point,&nbsp;nothing&nbsp;went&nbsp;right.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />}&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>If you know about how PHP handles <a href="http://php.net/language.variables.scope" target="_blank">scope</a>, you might be confused by the variable <code style="background-color: #FFFFBB">$options</code> in this function.  Normally, variables inside of functions are limited to that function: they &quot;disappear&quot; when the function completes, and are &quot;restarted&quot; every time the function is called.  That would make defining our $options <i>inside</i> the function basically useless: they'd be *gone* when the function ended.<br />
<br />
The <code style="background-color: #FFFFBB">static</code> operator changes that - the variable is still <i>accessible</i> only from the function's scope, but its contents are <i>preserved</i> when the function completes.  That's what allows us to use this function to &quot;look up&quot; values - the function remembers what is in $options between calls.  Interestingly, this allows us to <i>change</i> options in the middle of our program, or even <i>make up</i> new config settings - but we can explore that later.<br />
<br />
You might also wonder about our <code style="background-color: #FFFFBB">datasource</code>.  For this tutorial, we're going to use a MySQL database as our data source - many, many websites already use MySQL for <i>something</i>, so we'll take advantage of that by <i>passing</i> the database handle into our functions, rather than creating a new connection.  <br />
<br />
We mentioned <code style="background-color: #FFFFBB">restrict</code> earlier, and we'll mention <code style="background-color: #FFFFBB">deny</code> and <code style="background-color: #FFFFBB">hooks</code> later.  :)<br />
<br />
<br />
<font size="3"><i>Next Time</i></font><br />
<br />
In part two, we'll talk about how to check if a user is logged in or not, and we'll write the following functions:<br />
<span style="font-family: monospace"><ul><li style="">hedgeclipper_checkLoggedIn()</li><li style="">hedgeclipper_rememberLoggedIn()</li></ul></span><br />
We'll also write some HTML, so we have an idea of what our users are looking at on the front-end.<br />
<br />
I'll give you a week or so to stew, but in the meantime, Please Ask Questions!<br />
<br />
'Till then,<br />
<br />
<i>-Adrian</i></blockquote>

]]></content:encoded>
			<dc:creator>traq</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?273-A-Really-Big-PHP-Tutorial-part-1</guid>
		</item>
		<item>
			<title><![CDATA[Heaps o' Fun]]></title>
			<link>http://www.dynamicdrive.com/forums/entry.php?272-Heaps-o-Fun</link>
			<pubDate>Thu, 13 Dec 2012 07:08:12 GMT</pubDate>
			<description><![CDATA[yes, I'm still doing the tutorial.  : ) 
 
Did you know ...? 
 
 
PHP: 
--------- 
<?php 
 
class alphaHeap extends SplHeap{]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore"><font color="#808080"><i>yes, I'm still doing the tutorial.  : )</i></font><br />
<br />
<font size="3"><i>Did you know ...?</i></font><br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:336px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">alphaHeap&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">SplHeap</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">compare</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strcmp</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$alpha&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">alphaHeap</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">#&nbsp;IN&nbsp;put:<br /></span><span style="color: #0000BB">$alpha</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'cat'&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;C<br /></span><span style="color: #0000BB">$alpha</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'dog'&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;D<br /></span><span style="color: #0000BB">$alpha</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'boy'&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;B<br /></span><span style="color: #0000BB">$alpha</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'elephant'&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;E<br /></span><span style="color: #0000BB">$alpha</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'apple'&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;A<br /><br /></span><span style="color: #007700">foreach(&nbsp;</span><span style="color: #0000BB">$alpha&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">){&nbsp;print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$str</span><span style="color: #DD0000">&lt;br&gt;"</span><span style="color: #007700">;&nbsp;}<br /></span><span style="color: #FF8000">#OUT&nbsp;put:<br />/*<br />apple&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;A<br />boy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;B<br />cat&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;C<br />dog&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;D<br />elephant&nbsp;&nbsp;//&nbsp;E<br />*/</span>
</span>
</code></code></div>
</div><br />
<font size="3"><i>a Heap</i></font><br />
<br />
...is a data structure commonly used for sorting values.  They're very efficient (<b>edit</b>: 35%-40% faster, in fact).  I don't have an expert understanding, but here's <a href="http://en.wikipedia.org/wiki/Heap_(data_structure)" target="_blank">a good place to read more</a>.<br />
<br />
In practice, you might think of a Heap as an Ordered List.<br />
<br />
With normal PHP arrays, new values are inserted either according to their numeric keys, at a specific position using a complicated sequence of <code style="background-color: #FFFFBB">array_slice()</code>, <code style="background-color: #FFFFBB">array_push()</code> or <code style="background-color: #FFFFBB">array_unshift()</code>, and/or <code style="background-color: #FFFFBB">array_splice()</code>, or are (often) simply appended to the end of the array.  You have to be responsible for sorting these yourself, sometimes several times during script execution, which can lead to complicated loops or multi-dimensional indexing.  With a Heap, new values are &quot;sifted&quot; (compared and ordered) when they're inserted, so the Heap is always in the correct order.<br />
<br />
<br />
<font size="3"><i>the Standard PHP Library</i></font><br />
<br />
...is a collection of object classes that most PHP coders have never even heard of.  Shame, really.  There's some great stuff in there - like <b>Heaps</b>.<br />
<br />
The <a href="http://php.net/splheap" target="_blank">SplHeap</a> class is ready to go, with everything you need, save one lil' detail: how to order the data.  You have to define that yourself.<font color="#0000FF">*</font><br />
<br />
<font color="#0000FF">*</font><font color="#808080">The SPL also provides the <i>truly</i> ready-to-use SplMinHeap and SplMaxHeap classes, which order things (you guessed it!) smallest-first and largest-first, respectively.  But you can do such cool stuff with your own implementation!</font><br />
<br />
In my first example, I used the SplHeap's <a href="http://php.net/splheap.compare" target="_blank">::compare()</a> method to sort words alphabetically.  But you can do any number of other things.  Just extend the SplHeap class, and write your own version of compare() <font color="#808080">(<code style="background-color: #FFFFBB">compare()</code> must return <code style="background-color: #FFFFBB">-1</code> [less-than], <code style="background-color: #FFFFBB">0</code> [equal], or <code style="background-color: #FFFFBB">1</code> [greater-than])</font>.  Try these:<br />
<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">#&nbsp;biggest&nbsp;word&nbsp;first<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">bigWordHeap&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">SplHeap</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">compare</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">)?&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">:&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #FF8000">#&nbsp;fewest&nbsp;vowels&nbsp;first<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">fewestVowelsHeap&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">SplHeap</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">compare</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">){&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$A&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">numVowels</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$B&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">numVowels</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$A&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$B&nbsp;</span><span style="color: #007700">){&nbsp;return&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$A&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$B&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">:&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;function&nbsp;</span><span style="color: #0000BB">numVowels</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$vowels&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,</span><span style="color: #DD0000">'e'</span><span style="color: #007700">,</span><span style="color: #DD0000">'i'</span><span style="color: #007700">,</span><span style="color: #DD0000">'o'</span><span style="color: #007700">,</span><span style="color: #DD0000">'u'&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$letters&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_split</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$totalVowels&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(&nbsp;</span><span style="color: #0000BB">$letters&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$ltr&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$ltr</span><span style="color: #007700">,</span><span style="color: #0000BB">$vowels&nbsp;</span><span style="color: #007700">)&nbsp;){&nbsp;</span><span style="color: #0000BB">$totalVowels</span><span style="color: #007700">++;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$totalVowels</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #FF8000">#&nbsp;who&nbsp;knows&nbsp;first<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">randomOrderHeap&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">SplHeap</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">compare</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">){&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">rand</span><span style="color: #007700">(&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">,</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span>
</span>
</code></code></div>
</div><br />
<font size="3"><i>It Gets Better</i></font><br />
<br />
All well and good, right?  <i>&quot;But we already have the various sort() functions for arrays, and I already know how to use <i>those</i>.&quot;</i>  Check this out.<br />
<br />
Say you're getting info about a bunch of people from your database.<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:36px;">SELECT `name`,`age` FROM `people`</pre>
</div>You get your result set <font color="#808080">(and <font color="#B22222">NOT</font> by using the <a href="http://www.dynamicdrive.com/forums/showthread.php?71716-mysql_*-is-being-deprecated-!" target="_blank">mysql_*() functions</a>, of course)</font> and put it in an array.  For simplicity, we'll say you end up with something like this:<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:168px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">$example_resultSet&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'John'&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">22&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;,array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Mary'&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">44&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;,array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Albert'&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">48&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;,array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Vincent'&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">22&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;,array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Jane'&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">19&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;,array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Michael'&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">22&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;,array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Julia'&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">22&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;,array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Heather'&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">32&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;,array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'George'&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">25&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;,array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Erin'&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">48&nbsp;</span><span style="color: #007700">)<br />);&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>Each item in this example &quot;result set&quot; represents a &quot;row&quot; from our query.  Say you want to order everyone from oldest to youngest - but we seem to have a lot of people who are the same age.  Not being anarchists <br />
<br />
<div style="margin-left:40px"><font color="#808080">(<font size="-1">UNSORTED DATA!!?! <i>HERETIC!!!</i></font>)</font>,</div><br />
we decide that people who are the same age should be grouped and sorted alphabetically.<br />
<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ageGroupAlphaHeap&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">SplHeap</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">compare</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'age'</span><span style="color: #007700">&#93;&nbsp;===&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'age'</span><span style="color: #007700">&#93;&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strcmp</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">&#93;,</span><span style="color: #0000BB">$a</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">&#93;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'age'</span><span style="color: #007700">&#93;&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'age'</span><span style="color: #007700">&#93;?&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">:&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">#&nbsp;instantiate&nbsp;Heap<br /></span><span style="color: #0000BB">$sorted_resultSet&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ageGroupAlphaHeap</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">#&nbsp;insert&nbsp;each&nbsp;row&nbsp;from&nbsp;our&nbsp;*unsorted*&nbsp;result&nbsp;set<br /></span><span style="color: #007700">foreach(&nbsp;</span><span style="color: #0000BB">$example_resultSet&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">){&nbsp;</span><span style="color: #0000BB">$sorted_resultSet</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">);&nbsp;}<br /><br /></span><span style="color: #FF8000">#&nbsp;print&nbsp;out&nbsp;our&nbsp;*sorted*&nbsp;records<br /></span><span style="color: #007700">foreach(&nbsp;</span><span style="color: #0000BB">$sorted_resultSet&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$person&nbsp;</span><span style="color: #007700">){&nbsp;print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$person</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'age'</span><span style="color: #007700">&#93;}</span><span style="color: #DD0000">&nbsp;year-old&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$person</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">&#93;}</span><span style="color: #DD0000">.&lt;br&gt;"</span><span style="color: #007700">;&nbsp;}<br /><br /></span><span style="color: #FF8000">#&nbsp;Output:<br />/*<br />48&nbsp;year-old&nbsp;Albert.<br />48&nbsp;year-old&nbsp;Erin.<br />44&nbsp;year-old&nbsp;Mary.<br />32&nbsp;year-old&nbsp;Heather.<br />25&nbsp;year-old&nbsp;George.<br />22&nbsp;year-old&nbsp;John.<br />22&nbsp;year-old&nbsp;Julia.<br />22&nbsp;year-old&nbsp;Michael.<br />22&nbsp;year-old&nbsp;Vincent.<br />19&nbsp;year-old&nbsp;Jane.<br />*/</span>
</span>
</code></code></div>
</div>What's more, if we add another &quot;row&quot; later on, everything is still in proper order - <br />
<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:276px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$newRow&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Bethany'</span><span style="color: #007700">,</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">27&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$sorted_resultSet</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$newRow&nbsp;</span><span style="color: #007700">);<br /><br />foreach(&nbsp;</span><span style="color: #0000BB">$sorted_resultSet&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$person&nbsp;</span><span style="color: #007700">){&nbsp;print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$person</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'age'</span><span style="color: #007700">&#93;}</span><span style="color: #DD0000">&nbsp;year-old&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$person</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'name'</span><span style="color: #007700">&#93;}</span><span style="color: #DD0000">.&lt;br&gt;"</span><span style="color: #007700">;&nbsp;}<br /><br /></span><span style="color: #FF8000">#&nbsp;Output:<br />/*<br />48&nbsp;year-old&nbsp;Albert.<br />48&nbsp;year-old&nbsp;Erin.<br />44&nbsp;year-old&nbsp;Mary.<br />32&nbsp;year-old&nbsp;Heather.<br />27&nbsp;year-old&nbsp;Bethany.<br />25&nbsp;year-old&nbsp;George.<br />22&nbsp;year-old&nbsp;John.<br />22&nbsp;year-old&nbsp;Julia.<br />22&nbsp;year-old&nbsp;Michael.<br />22&nbsp;year-old&nbsp;Vincent.<br />19&nbsp;year-old&nbsp;Jane.<br />*/</span>
</span>
</code></code></div>
</div>...and <i>we</i> got to be lazy about it.  :)<br />
<br />
<br />
<font size="3"><i>Enjoy</i></font><br />
<br />
SPL also has other interesting and useful data structures, iteritors, functions, and classes.  <a href="http://php.net/spl" target="_blank">Check them out</a>.  You might find something awesome for your next project.<br />
<br />
Please Ask Questions,<br />
<br />
-Adrian</blockquote>

]]></content:encoded>
			<dc:creator>traq</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?272-Heaps-o-Fun</guid>
		</item>
		<item>
			<title>New windows, new tabs and popup blockers</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?271-New-windows-new-tabs-and-popup-blockers</link>
			<pubDate>Fri, 07 Dec 2012 21:33:22 GMT</pubDate>
			<description><![CDATA[*(To see and test what this is all about, you should download Safari  if you haven't already done so. After that, use the code below with popup blocker enabled AND with popup blocker disabled, using Safari)*. 
 
When a new window or tab is not explicitly requested by the visitor of a site i.e. when...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore"><b>(To see and test what this is all about, you should download <i>Safari </i> if you haven't already done so. After that, use the code below with popup blocker enabled AND with popup blocker disabled, using Safari)</b>.<br />
<br />
When a new window or tab is not explicitly requested by the visitor of a site i.e. when the window's appearance is attached to such events as <i>onload</i> or <i>onunload</i>, popup blockers (if enabled) will prevent a file from being opened. But when the window's appearance is attached to an <i>onclick</i> event on a link (and other events that can be taken to mean that a person has explicitly requested a new window, like the <i>onchange</i> in a select box, see below), the browser won't allow popup blockers to stop windows or tabs from appearing.<br />
<br />
Well, not quite so. Sometimes, browsers / popup blockers make mistakes and block windows or tabs although they are explicitly requested by the user. One such example is <i>Safari</i>'s treatment of links in the options of a select box. A simple example of a script for opening a new window or tab using the <i>onclick</i> event is the following:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:288px;">&lt;script&gt;
function open_in_new_window(url)
{
var wwidth=700;
var lleft=parseInt(screen.width/2)-parseInt(wwidth/2);
the_popup=window.open(url,&quot;_blank&quot;,&quot;left=&quot;+lleft+&quot;,width=&quot;+wwidth+&quot;,height=400, top=100&quot;);
}

function open_in_new_tab(url)
{window.open('','_new').location.replace(url)}
&lt;/script&gt;


&lt;select onchange=&quot;selectedIndex=0; open_in_new_window('http://www.dynamicdrive.com');&quot;&gt;
&lt;option disabled=&quot;disabled&quot; selected&gt;Destination in new window&lt;/option&gt;
&lt;option &gt;Open in new window&lt;/option&gt;
&lt;/select&gt;

&lt;select onchange=&quot;selectedIndex=0; open_in_new_tab('http://www.dynamicdrive.com')&quot;&gt;
&lt;option disabled=&quot;disabled&quot; selected&gt;Destination in new tab&lt;/option&gt;
&lt;option &gt;Open in new tab&lt;/option&gt;
&lt;/select&gt;</pre>
</div>A click on the second option of both select boxes above should open a new window (first select box) or a new tab (second select box). And that's indeed what happens in <i>IE, Firefox, Chrome</i> and <i>Opera</i> (popup blockers enabled!). But not so with <i>Safari</i>. This browser does not recognize <i>onchange=open_in_new_window(...)</i> and <i>onchange=open_in_new_tab(...)</i> as explicit requests. In this particular case, it views the <i>onchange</i> as something that is forced upon the user and that, therefore, must be prevented to happen.<br />
<br />
There may be other cases where things like this may happen (with <i>Safari</i> or with other browsers). So we must have a means to warn the user to disable his/her popup blocker in particular cases. The code is simple. We must simply verify whether or not the browser 'recognizes' or allows an event. In our example, <i>Safari</i> apparently does not recognize or allow <i>window.open</i> and <i>window.open('','_new')</i> (if blocking popups is enabled). We can use this information to modify the script given above as follows:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:264px;">&lt;script&gt;
function open_in_new_window(url)
{
var wwidth=700;
var lleft=parseInt(screen.width/2)-parseInt(wwidth/2);
the_popup=window.open(url,&quot;_blank&quot;,&quot;left=&quot;+lleft+&quot;,width=&quot;+wwidth+&quot;,height=400, top=100&quot;);

if (typeof(the_popup)=='undefined'){alert(&quot;Your browser tried to open:\n\n&quot;+unescape(url)+&quot;\n\nAlthough this is a safe file, your security settings prevented it from being opened in a new window. Please disable your pop-up blocker, reload the page, then click the link again.&quot;); }

}

function open_in_new_tab(url)
{

if (typeof(window.open('','_new'))=='undefined') {
alert(&quot;Your browser tried to open:\n\n&quot;+unescape(url)+&quot;\n\nAlthough this is a safe file, your security settings prevented it from being opened in a new tab. Please disable your pop-up blocker, reload the page, then click the link again.&quot;); }

window.open('','_new').location.replace(url)
}
&lt;/script&gt;</pre>
</div><br />
DEMO AND EXPLANATIONS<a href="http://mesdomaines.nu/eendracht/window_open_with_blocker/window_open_with_blocker.html" target="_blank"><b><i> here</i></b></a> (you must use <i> Safari</i>).<br />
<br />
Arie</blockquote>

]]></content:encoded>
			<dc:creator>molendijk</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?271-New-windows-new-tabs-and-popup-blockers</guid>
		</item>
		<item>
			<title>Not My Type... : )</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?270-Not-My-Type-)</link>
			<pubDate>Thu, 29 Nov 2012 05:13:42 GMT</pubDate>
			<description><![CDATA[Yes, the tutorial is still coming.  It's big.  It's in progress.  :) 
 
I read this recently (http://nikic.github.com/2012/03/06/Scalar-type-hinting-is-harder-than-you-think.html) and was intrigued.  Don't know if anyone else would care or not ...:) 
 
The author, nikic...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore"><font color="#808080"><i>Yes</i>, the tutorial is still coming.  It's big.  It's in progress.  :)</font><br />
<br />
<font size="3"><a href="http://nikic.github.com/2012/03/06/Scalar-type-hinting-is-harder-than-you-think.html" target="_blank">I read this recently</a></font> and was intrigued.  Don't know if anyone else would care or not ...:)<br />
<br />
The author, <a href="http://nikic.github.com/aboutMe.html" target="_blank">nikic</a>, does a great job summarizing the difficulties and potential benefits of <b>type hinting</b>, as well as giving a good explanation as to why PHP's implementation is incomplete.  Personally, I look forward to being able to type-hint any type.  It could save me a lot of code <i>validating</i> args, and I could focus more on actually writing code.<br />
<br />
<font size="3"><i>Lost?</i></font><br />
<br />
If you're not familiar with what is being talked about...<br />
<br />
PHP <font color="#808080">(javascript too, coincidentially, but of course it's implemented differently)</font> is <b>loosely typed</b>.<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:120px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">#&nbsp;this&nbsp;is&nbsp;a&nbsp;string&nbsp;(a&nbsp;_text&nbsp;character_,&nbsp;no&nbsp;different&nbsp;than&nbsp;'a',&nbsp;'b',&nbsp;or&nbsp;'c').<br /></span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'5'</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">#&nbsp;this&nbsp;is&nbsp;an&nbsp;integer&nbsp;(a&nbsp;_whole&nbsp;number_,&nbsp;like&nbsp;1,&nbsp;2,&nbsp;or&nbsp;3).<br /></span><span style="color: #0000BB">$int&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /><br />print&nbsp;</span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$int</span><span style="color: #007700">;</span>
</span>
</code></code></div>
</div>In a &quot;strictly typed&quot; language <font color="#808080">(like C++ or Java, for example)</font>, this would result in an error:<br />
<font color="#fafafa">.....</font><code style="background-color: #FFFFBB">Error ID:10T [you can't do math with sentences]</code><br />
<br />
or something like that.  But PHP does somthing called &quot;type juggling&quot;: when it needs a number, it does its darndest to _have_ a number.  Since &quot;5&quot; is reasonably equivalent to 5, it <b>casts</b> the string &quot;5&quot; to an integer (5).  The string becomes an integer: literally, silently, automatically; and the output <i>is</i> as <i>expected</i>:<br />
<font color="#fafafa">.....</font><code style="background-color: #FFFFBB">10</code><br />
<br />
PHP is loosely typed for a very, very important reason: it's a web language.  Data comes and goes across the internet.  Data comes and goes across the internet <font color="#8f0e0e"><i>as strings</i></font>.<br />
<br />
That's right, there's no such thing as an &quot;integer&quot; over HTTP.  When you type in a URL, it's sent as text.  When you submit a form, it's sent as text.  Even if it's all numbers.  Every $_GET or $_POST variable that PHP populates starts out as a string.  But, you might need to do math with them, and thanks to type juggling you can.<br />
<br />
<font color="#808080"><i>(Incidentially, type juggling is also the reason you can do <code style="background-color: #FFFFBB">if( $variable )</code>.  <code style="background-color: #FFFFBB">if()</code> needs a boolean (TRUE/FALSE) value, so PHP type-juggles and casts <code style="background-color: #FFFFBB">$variable</code> to a boolean.  <br />
<br />
(<a href="http://php.net/language.types.type-juggling" target="_blank">The rules about this process</a> are kinda interesting, and the results sometimes more so.  For example:<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:252px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$int&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">10000</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;integer&nbsp;(10,000)<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"10,000"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;string&nbsp;"10,000"<br /></span><span style="color: #0000BB">$sum&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$int&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;integer&nbsp;(10,010)<br /><br /></span><span style="color: #0000BB">$int&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;integer&nbsp;(5)<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"5"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;string&nbsp;"5"<br /></span><span style="color: #0000BB">$int&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;string&nbsp;"55"<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,</span><span style="color: #DD0000">'c'&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;array&nbsp;(3)<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'d'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;string&nbsp;"Arrayd"<br /><br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"No"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;string&nbsp;"No"<br /></span><span style="color: #0000BB">$bool&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;boolean&nbsp;(TRUE)<br /></span><span style="color: #0000BB">$sum&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$bool</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;integer&nbsp;(1)<br /></span><span style="color: #0000BB">$sen&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">.</span><span style="color: #0000BB">$bool</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;string&nbsp;"No1"<br /><br /></span><span style="color: #007700">if(&nbsp;</span><span style="color: #DD0000">"FALSE"&nbsp;</span><span style="color: #007700">){&nbsp;</span><span style="color: #FF8000">/*&nbsp;true.&nbsp;*/&nbsp;</span><span style="color: #007700">}</span>
</span>
</code></code></div>
</div>(...some of these might seem ...logical... once you think about it for a while.  But did you really expect to add a string to a boolean and get an integer?  Anyway... this is a different discussion.)</i></font><br />
<br />
<font size="3"><i>Need a Hint?</i></font><br />
<br />
Along with type juggling and type casting, PHP 5.1 introduced <b>type hinting</b>, and 5.4 has added more to it.  Basically, you can make your functions require a particular type of argument.  Here's a function that needs an array:<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:108px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">loop_d_loo</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"looping&nbsp;through&nbsp;array:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach(&nbsp;</span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$item&nbsp;</span><span style="color: #007700">){&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;do&nbsp;something&nbsp;loopy&nbsp;*/&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />}</span>
</span>
</code></code></div>
</div>but what if you forget?<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:72px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />loop_d_loo</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">42&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">#&nbsp;&gt;&nbsp;looping&nbsp;through&nbsp;array:<br />#&nbsp;&gt;&nbsp;Warning:&nbsp;Invalid&nbsp;argument&nbsp;supplied&nbsp;for&nbsp;foreach()&nbsp;...</span>
</span>
</code></code></div>
</div>But wait ...!  You can use type hinting:<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:144px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">loop_d_loo</span><span style="color: #007700">(&nbsp;array&nbsp;</span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"looping&nbsp;through&nbsp;array:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach(&nbsp;</span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$item&nbsp;</span><span style="color: #007700">){&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;do&nbsp;something&nbsp;loopy&nbsp;*/&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />}<br /><br /></span><span style="color: #0000BB">loop_d_loo</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">42&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">#&nbsp;&gt;&nbsp;Catchable&nbsp;fatal&nbsp;error:&nbsp;Argument&nbsp;1&nbsp;passed&nbsp;to&nbsp;loop_d_loo()&nbsp;must&nbsp;be&nbsp;of&nbsp;the&nbsp;type&nbsp;array,&nbsp;integer&nbsp;given&nbsp;...</span>
</span>
</code></code></div>
</div>You might say &quot;an error is an error,&quot; and you'd be right, but the error in the second case is preferable for two reasons:<br />
<ol class="decimal"><li style="">The function is not executed <i>at all</i>, leaving no &quot;broken&quot; data or half-baked output.</li><li style="">The error message is more descriptive and more accurately describes where the problem really came from, leading to better bug-fixin'.<br />
<font color="#808080">Actually, with <a href="http://php.net/language.exceptions" target="_blank">exception based error handling</a> and <code style="background-color: #FFFFBB"><a href="http://php.net/set_error_handler" target="_blank">set_error_handler()</a></code>, you can deal with the problem at runtime (by skipping it, or even casting the bad argument to the proper type or substituting a default value).</font></li></ol><br />
<br />
PHP 5.1+ allows you to hint that your argument needs to be an object (even of a particular class, or using a particular interface) or an array. Version 5.4 allows you to require an argument to be <b>callable</b> (a function/method or name of a function/method).<br />
<br />
<font size="3"><i>Back To Basics</i></font><br />
<br />
You'll notice that the basic types, like integer, string, boolean, and so forth, are missing: you cannot use type hinting for <b>scalar</b> types <font color="#808080">(a &quot;scalar&quot; type is one that can only hold one value at a time, as opposed to arrays or objects, which can hold multiple values)</font>.  You might think that scalar type hinting would be <i>easier</i> than with other types - that's the misconception that <a href="http://nikic.github.com/2012/03/06/Scalar-type-hinting-is-harder-than-you-think.html" target="_blank">the article I referred to earlier</a> was discussing.<br />
<br />
The non-scalar types are easier to &quot;hint at&quot; because they aren't easily type-juggled - and under normal circumstances, no one would want them to be.  <font color="#808080">(Why would you want to treat an object that contains a dozen properties (values) as a single value?  How would it even make sense to try?)</font>  I have to admit that there are complications I never even thought of, but the author's favorite solution is the one I had in mind before I started reading: Strict Weak Hinting (with casts).<br />
<br />
I think it would be quite useful if you could specify that you wanted a integer, and, given an argument <i>looked</i> like an integer, you would <i>get</i> one.  <br />
<br />
This would involve type juggling and casting working together: <b>if</b> the argument can be cast to the hinted type, <b>and</b> there is no loss of data, <b>then</b> cast it to the hinted type.  Otherwise, throw an error.  For example:<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:156px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">#&nbsp;NOT&nbsp;REAL&nbsp;PHP&nbsp;#<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">intHint</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">int&nbsp;$int&nbsp;</span><span style="color: #007700">){&nbsp;</span><span style="color: #FF8000">/*&nbsp;do&nbsp;us&nbsp;some&nbsp;maths&nbsp;*/&nbsp;</span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">intHint</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;integer&nbsp;(1):&nbsp;we're&nbsp;all&nbsp;good.<br /></span><span style="color: #0000BB">intHint</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"1"&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;string&nbsp;"1"&nbsp;-&nbsp;but&nbsp;equivalent&nbsp;to&nbsp;(1),&nbsp;so&nbsp;cast&nbsp;it&nbsp;and&nbsp;we're&nbsp;all&nbsp;good.<br /></span><span style="color: #0000BB">intHint</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">1.5&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float&nbsp;(1.5)&nbsp;-&nbsp;casting&nbsp;to&nbsp;int&nbsp;would&nbsp;give&nbsp;(1),&nbsp;which&nbsp;is&nbsp;*not*&nbsp;equivalent&nbsp;to&nbsp;1.5:&nbsp;throw&nbsp;an&nbsp;error.<br /></span><span style="color: #0000BB">intHint</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"one"&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;string&nbsp;"one"&nbsp;-&nbsp;would&nbsp;be&nbsp;cast&nbsp;to&nbsp;(0),&nbsp;which&nbsp;is&nbsp;not&nbsp;equivalent:&nbsp;throw&nbsp;an&nbsp;error.<br /></span><span style="color: #0000BB">intHint</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool&nbsp;(TRUE)&nbsp;-&nbsp;casting&nbsp;to&nbsp;int&nbsp;would&nbsp;give&nbsp;(1),&nbsp;which&nbsp;is&nbsp;equivalent&nbsp;-&nbsp;but...&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;I'm&nbsp;not&nbsp;sure&nbsp;about&nbsp;this&nbsp;one.</span>
</span>
</code></code></div>
</div><font size="3"><i>In Conclusion</i></font><br />
<br />
That's my random thoughts for the day.  Anyone care to share theirs?<br />
<br />
Happy coding,<br />
<br />
- Adrian</blockquote>

]]></content:encoded>
			<dc:creator>traq</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?270-Not-My-Type-)</guid>
		</item>
		<item>
			<title>Before You Start: the basics, for PHP in particular and for programming in general</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?269-Before-You-Start-the-basics-for-PHP-in-particular-and-for-programming-in-general</link>
			<pubDate>Fri, 23 Nov 2012 07:18:59 GMT</pubDate>
			<description><![CDATA[I wanted to take a few moments to talk about some of "the basics" that are all-to-often glossed over... 
 
A Matter of Style 
 
In general, I find that most PHP programmers fall into one of three main programming styles: 
 
* procedural: a simple ordered set of instructions, nested if's/ else's,...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">I wanted to take a few moments to talk about some of &quot;the basics&quot; that are all-to-often glossed over...<br />
<br />
<font size="4"><i>A Matter of Style</i></font><br />
<br />
In general, I find that most PHP programmers fall into one of three main programming styles:<br />
<ul><li style="">procedural: a simple ordered set of instructions, nested <code style="background-color: #FFFFBB">if</code>'s/ <code style="background-color: #FFFFBB">else</code>'s, etc.</li><li style="">functional: <b>functions</b> are little code containers that you can call on when needed.  You [usually] give them arguments, they [usually] return values to you.</li><li style="">object-oriented: <b>objects</b> wrap up a set of functions and related information (<i>properties</i>) to represent a complete &quot;thing&quot; in your program.  You have whatever objects you need, and you manipulate them using their <i>methods</i> (functions).</li></ul><br />
<br />
I'm a big fan of Object-Oriented Programming.  Objects are very flexible and allow you to do things you'd never have thought possible otherwise.  Even for tasks that could be accomplished without Objects, OOP is really cool and builds lots of good programming habits.<br />
<br />
<b>There's style, and then there's Style.</b><br />
<br />
When it comes to writing your program, how you do it is important.<br />
<br />
<ul><li style="">Use a good <b>plain text editor</b> with (at a minimum) syntax highlighting.  Some recommendations:<br />
    <ul><li style=""><a href="http://notepad-plus-plus.org/" target="_blank">Notepad++</a> (Windows)</li><li style=""><a href="http://www.barebones.com/products/textwrangler/download.html" target="_blank">TextWrangler</a> (Mac)</li><li style=""><a href="http://projects.gnome.org/gedit/" target="_blank">gEdit</a> (Win/Mac/Linux)</li><li style=""><a href="http://www.activestate.com/komodo-edit" target="_blank">Komodo Edit</a> (Win/Mac/Linux) <br />
<font color="#808080">(My favorite, by far. <b>I highly recommend it</b>.)</font></li></ul><br />
Microsoft Word <b>is not</b> a good choice for programming.  Less obviously, programs like DreamWeaver and FrontPage often produce buggy, poor-performing sites.  Quite simply, they're made for people who don't know what they're doing: you sacrifice control and quality for convenience.<br />
<font color="#fafafa">.</font></li><li style=""><b>Format your code</b>.  You're not &quot;wasting space&quot; by indenting your code.  You're not &quot;slowing down the program&quot; by adding comments.  You're not being an &quot;eLiTe haX0r&quot; by writing code so &quot;clever&quot; that no one has any hope of deciphering it later.</li></ul><br />
Compare:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:180px;">    function find( $find,$val=null )
{
$recurse = function( $obj )use( &amp;$prop,$val,&amp;$recurse )
{
              $p = array_shift( $prop ); return isset( $obj-&gt;$p )? empty( $prop )? is_null( $val )? true: ($obj-&gt;$p === $val): $recurse( $obj-&gt;$p ): false;
};
   foreach( $this as $obj )
{ $prop = strpos( $find,'.' )? explode( '.',$find ): (array)$find;
if( $recurse( $obj ) )
{ return $obj; 
}
}return false;          
}</pre>
</div><div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;searches&nbsp;storage&nbsp;for&nbsp;an&nbsp;object&nbsp;containing&nbsp;property&nbsp;$find;&nbsp;optionally&nbsp;with&nbsp;value&nbsp;===&nbsp;$val<br />&nbsp;*&nbsp;@param&nbsp;string&nbsp;$find&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;of&nbsp;property&nbsp;to&nbsp;find&nbsp;(in&nbsp;the&nbsp;form&nbsp;`object.property`)<br />&nbsp;*&nbsp;@param&nbsp;mixed&nbsp;$val&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;to&nbsp;match&nbsp;property&nbsp;against<br />&nbsp;*&nbsp;@returns&nbsp;bool|object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;object&nbsp;if&nbsp;match&nbsp;found;&nbsp;false&nbsp;otherwise<br />&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">find</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$find</span><span style="color: #007700">,</span><span style="color: #0000BB">$val</span><span style="color: #007700">=</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**&nbsp;closure&nbsp;bool&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;recurses&nbsp;through&nbsp;an&nbsp;object-&gt;property&nbsp;chain&nbsp;to&nbsp;search&nbsp;for&nbsp;property.&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$recurse&nbsp;</span><span style="color: #007700">=&nbsp;function(&nbsp;</span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">)use(&nbsp;&amp;</span><span style="color: #0000BB">$prop</span><span style="color: #007700">,</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&amp;</span><span style="color: #0000BB">$recurse&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$p&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_shift</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$prop&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;isset(&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$p&nbsp;</span><span style="color: #007700">)?<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;empty(&nbsp;</span><span style="color: #0000BB">$prop&nbsp;</span><span style="color: #007700">)?<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">is_null</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">)?<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$p&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$recurse</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$p&nbsp;</span><span style="color: #007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">false<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;};<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;search&nbsp;stored&nbsp;objects&nbsp;for&nbsp;matching&nbsp;property&nbsp;&#91;&amp;&nbsp;value&#93;;&nbsp;return&nbsp;if&nbsp;found<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">foreach(&nbsp;</span><span style="color: #0000BB">$this&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;parse&nbsp;property&nbsp;chain<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$prop&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$find</span><span style="color: #007700">,</span><span style="color: #DD0000">'.'&nbsp;</span><span style="color: #007700">)?&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'.'</span><span style="color: #007700">,</span><span style="color: #0000BB">$find&nbsp;</span><span style="color: #007700">):&nbsp;(array)</span><span style="color: #0000BB">$find</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;recurse<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">$recurse</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">)&nbsp;){&nbsp;return&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#&nbsp;no&nbsp;matches&nbsp;found<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>Take it to heart.<br />
<br />
<font size="4"><i>Server Set-Up</i></font><br />
<br />
I'm running PHP <a href="http://php.net/manual/en/migration54" target="_blank">version 5.4</a>.  You should too.  <b>It's fantastic</b>.  <br />
<br />
However, there's nothing wrong with version 5.3.  It came out in 2009, so there's no excuse to not have some release of it.  If you're still on 5.2, you really need to upgrade.  If your web host doesn't offer 5.3+, you should be looking for a new host.<br />
<br />
<div style="border:1px solid red; width: 95%; padding: 5px;"><b>Edit:</b> <i>I know many people are stuck with older codebases.  <br />
5.2 is &quot;okay,&quot; if you're not in a position to leave it behind.  But your goal should be to upgrade.</i></div><br />
<br />
If you don't have 5.4, check these settings in your <span style="font-family: monospace">php.ini</span> file.  <u>Make sure</u>:<br />
<ul><li style="">safe mode is turned OFF</li><li style="">register_globals is turned OFF</li><li style="">register_long_arrays is turned OFF</li><li style="">magic_quotes_gpc is turned OFF</li></ul><br />
These things aren't safe at all (no, not even safe mode).  <b>They cause problems.  <font color="#B22222">Don't use them</font>.</b><br />
<i>(Talk to your web host.)</i><br />
<br />
<font size="4"><i>Fun and Games and Error Messages</i></font><br />
<br />
We should have a brief word about <i>development</i> vs. <i>production</i>:<br />
<ol class="decimal"><li style=""><u>development</u> is when you are writing and testing your code.<br />
<font color="#fafafa">.</font><br />
When you're in <i>development</i>, you want to see <b>every last error message</b>.  You <i>want</i> your program to <i>crash and burn</i> at every little bump in the road.  You <i>want</i> PHP to complain endlessly about absolutely <i>everything</i>.  <br />
<font color="#fafafa">.</font><br />
Why?  ...why indeed.<br />
<font color="#fafafa">.</font></li><li style=""><u>production</u> is when you put your code up on your website for all to see.<br />
<font color="#fafafa">.</font><br />
When you're in <i>production</i>, it's completely different.  If there's a problem, you want to <b>hide it at all costs</b>.  <br />
<font color="#fafafa">.</font><br />
Error messages?  <b>NO!</b><br />
If it's so bad a problem that you can't recover, <a href="http://cdn.smosh.com/sites/default/files/bloguploads/dinosaur_404_by_deathpoint-d46y0uq.jpg" target="_blank">blame the dinosaurs</a> - not <a href="http://www.mactech.com/articles/mactech/Vol.20/20.03/ProgrammingandMySQL/figure01.jpg" target="_blank">your program</a>.  No one &quot;hacks&quot; dinosaurs.  But if you mention that something went wrong because {function} couldn't connect to {host} on {socket} with {username} and {variable} had an unexpected {value}, you can bet someone, somewhere, will start hacking away .</li></ol><br />
<br />
In the spirit of Development, use these settings in your <span style="font-family: monospace">php.ini</span> file:<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:60px;">error_reporting = <code style="background-color: #FFFFBB">E_ALL &amp; E_STRICT</code>
display_errors = <code style="background-color: #FFFFBB">On</code>
display_startup_errors = <code style="background-color: #FFFFBB">On</code></pre>
</div>You can also do this at the top of your script:<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:60px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(&nbsp;-</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'display_errors'</span><span style="color: #007700">,</span><span style="color: #DD0000">'On'&nbsp;</span><span style="color: #007700">);</span>
</span>
</code></code></div>
</div>However, it's not as useful, and it won't always work.  : (<br />
<br />
Remember, in Production, both <code style="background-color: #FFFFBB">display_errors</code> and <code style="background-color: #FFFFBB">display_startup_errors</code> should be <code style="background-color: #FFFFBB">Off</code>.<br />
<br />
<font size="4"><i>I Did Everything Right and My Code Still Broke</i></font><br />
<br />
This is not unusual.<br />
<font color="#808080">(<i>Everyone</i> thinks they did everything right.)</font><br />
No, I believe you.<br />
Really.<br />
<br />
<b>Situation #1</b><br />
<br />
Something is wrong with your code.  You're getting all kinds of crazy error messages.  They say obscure things like <br />
<div class="bbcode_container">
	<div class="bbcode_quote">
		<div class="quote_container">
			<div class="bbcode_quote_container"></div>
			
				<b>Parse error:</b> syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ')' in ...
			
		</div>
	</div>
</div>or<br />
<div class="bbcode_container">
	<div class="bbcode_quote">
		<div class="quote_container">
			<div class="bbcode_quote_container"></div>
			
				<b>Warning:</b> [function.session-start]: Cannot send session cache limiter - headers already sent ...
			
		</div>
	</div>
</div>Don't Panic.<br />
<br />
Error messages are your friends.<br />
<br />
Quite often, they are simply the result of typos.  Just read the message - it tells you <i>exactly</i> what's wrong.  If you don't understand something in the message, <a href="http://lmgtfy.com/?q=unexpected+T_PAAMAYIM_NEKUDOTAYIM" target="_blank">google it</a>.  You're all big boys and girls; you're allowed to use the internet on your own.<br />
<br />
If the problem is not obvious, and the 'net fails you, then sit back and take a good look at your code.  Start at the line number the error message gave you, and <b>work backwards</b>.  (There's a fundamental truth to be uncovered here: PHP doesn't actually know where the problem was <i>caused</i>; it knows only where the problem got <i>unfixable</i>.)<br />
<br />
For example:<br />
<div class="bbcode_container">
	<div class="bbcode_quote">
		<div class="quote_container">
			<div class="bbcode_quote_container"></div>
			
				<b>Parse error:</b> syntax error, unexpected 'print' (T_PRINT) ...on line 2
			
		</div>
	</div>
</div><div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:36px;"><font color="#808080">2 &gt;</font>   print &quot;hello&quot;;</pre>
</div>That looks fine, right?  Hint: back up.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:36px;"><font color="#808080">1 &gt;</font>   some_function( 'this does cool stuff' )  // &lt;---WHUP NO CLOSING SEMICOLON</pre>
</div>problem solved.<br />
<br />
<b>Situation #2</b>:<br />
<br />
Something is wrong with your code.  You're not getting any error messages at all.  But your webpage says &quot;Vote For The Cutest Kitten&quot; AND THERES ABSOLUTELY NO CATS ANYWHERE.  Maybe there is no error - maybe what you wrote just doesn't do what you expected.<br />
<br />
Don't Panic.<br />
<br />
This is where <b>version control</b> come into play.  &quot;Undo&quot; is just not good enough when it comes to managing your code.  Ya know how people tell ya, &quot;backup your files&quot;?  If you don't want to get into using version control systems,* like <a href="http://subversion.apache.org/" target="_blank">subversion</a> or <a href="http://git-scm.com/" target="_blank">git</a>, you need to at least make it a habit to &quot;backup your files&quot; every time you make a significant change.  I don't mean having several copies of the latest version - you should be able to &quot;revert&quot; to any of the last two or three working versions of your scripts.<br />
<br />
<font color="#808080">*Yes, I said &quot;if you want to.&quot;  There are people who will be mad at me because I didn't say you <i>have</i> to.  However, I <i>will</i> say that, if you're doing anything more than &quot;messing around,&quot; you really, really should be using a VCS.  It's well worth it.</font><br />
<br />
<font size="4"><i>Where Do We Go From Here?</i></font><br />
<br />
We're going to build a Web App.  All kinds of bells and whistles and shiney stuff.  Coming soon to a blog near you.  Look for it.<br />
<br />
PAQ,<br />
- Adrian</blockquote>

]]></content:encoded>
			<dc:creator>traq</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?269-Before-You-Start-the-basics-for-PHP-in-particular-and-for-programming-in-general</guid>
		</item>
		<item>
			<title>Taking Control</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?268-Taking-Control</link>
			<pubDate>Fri, 09 Nov 2012 02:22:52 GMT</pubDate>
			<description><![CDATA[Last week, I wrote about taking a "PHP-first" approach to programming - program first, output last.  But what should go first in your program? 
 
M-V-C 
You may have heard about something called "MVC" (*M*odel-*V*iew-*C*ontroller).  If not, here's your crash course: 
 
*Model* 
 
Your Model is all...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Last week, I wrote about taking a &quot;PHP-first&quot; approach to programming - program first, output last.  But what should go first in your program?<br />
<br />
<font size="4"><i>M-V-C</i></font><br />
You may have heard about something called &quot;MVC&quot; (<b>M</b>odel-<b>V</b>iew-<b>C</b>ontroller).  If not, here's your crash course:<br />
<br />
<b>Model</b><br />
<br />
Your <i>Model</i> is all of the information needed/used by your program.  [Hopefully], the Model is well-organized.  Think of it in terms of note-taking: notes are easier to read when they're organized by subject, sub-subject, etc. than when they're just scribbled haphazardly all over the margins of your textbook.  <br />
<br />
<i>(Yes, I know that some people - and yes, I'm one of them - actually function better with a huge glob of cryptic post-its ...or we seem to prefer things that way, at least... but we do not think like computers.  </i>Computers don't like post-its.<i>  Trust me.)</i><br />
<br />
<b>View</b><br />
<br />
Your <i>View</i> is how all that information is supposed to be displayed.  In our domain - Web Design - it's actually very easy to figure out what belongs in the View:  output!  If there's HTML, then it belongs in the View.  Likewise, CSS, JavaScript, even HTTP Headers, etc., belong here.<br />
<br />
Learning &quot;PHP-first&quot; helps by separating all of the output from the rest of the program.  However, consider that the View is not <i>the output itself</i>, but all of the stuff <i>that is going to be</i> output.  An example, where we show a comment on a page:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:252px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;Model:&nbsp;the&nbsp;information&nbsp;we&nbsp;need<br /></span><span style="color: #007700">list(&nbsp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">,</span><span style="color: #0000BB">$author</span><span style="color: #007700">,</span><span style="color: #0000BB">$publish_date&nbsp;</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">get_comment</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$commentID&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;(`$commentID`&nbsp;is&nbsp;some&nbsp;identifier&nbsp;for&nbsp;the&nbsp;comment&nbsp;we&nbsp;want&nbsp;(e.g.,&nbsp;a&nbsp;DB&nbsp;record&nbsp;id))<br /><br /><br />//&nbsp;View:&nbsp;not&nbsp;output&nbsp;*yet*,&nbsp;but&nbsp;it&nbsp;*will&nbsp;be*!<br /></span><span style="color: #0000BB">$comment&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;&nbsp;HTML<br /></span><span style="color: #DD0000">&nbsp;&nbsp;&nbsp;&nbsp;&lt;article&nbsp;class="comment"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;</span><span style="color: #0000BB">$message</span><span style="color: #DD0000">&lt;/p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&nbsp;class="author"&gt;Published&nbsp;by&nbsp;</span><span style="color: #0000BB">$author</span><span style="color: #DD0000">&nbsp;on&nbsp;</span><span style="color: #0000BB">$publish_date</span><span style="color: #DD0000">.&lt;/p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/article&gt;<br /></span><span style="color: #007700">HTML<br />;<br /><br /></span><span style="color: #FF8000">//&nbsp;View?&nbsp;&nbsp;no...&nbsp;&nbsp;&nbsp;than&nbsp;what?<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$comment</span><span style="color: #007700">;</span>
</span>
</code></code></div>
</div><b>Controller</b><br />
<br />
The <i>Controller</i> is the missing part of the plan.  The Controller controls everything.  It's the parts of your script that check conditions, make decisions, and order things to be done.  In the above example, the last line is part of the controller: it tells the script what to do (i.e., print something).  There is probably some other Controller code [not shown in the example] that decided to build the comment in the first place - some sort of <code style="background-color: #FFFFBB">if( this ){ then; }else{ that; }</code>.<br />
<br />
So, any given PHP script probably has two types of Controller code:<br />
<ul><li style=""> code that makes decisions, and</li><li style=""> instructions for carrying out decisions.</li></ul><br />
<br />
With a Controller, our example might look something like this:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:276px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;controller&nbsp;logic&nbsp;(decide&nbsp;what&nbsp;to&nbsp;do):<br />//&nbsp;&nbsp;&nbsp;IF&nbsp;the&nbsp;user&nbsp;asked&nbsp;for&nbsp;a&nbsp;comment,<br /></span><span style="color: #007700">if(&nbsp;!empty(&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'comment_id'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;procedure&nbsp;(instructions&nbsp;on&nbsp;what&nbsp;to&nbsp;do):<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;get&nbsp;the&nbsp;comment&nbsp;they&nbsp;want.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$model&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getComment</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'id'</span><span style="color: #007700">&#93;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$view&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">prepareComment</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$model&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;controller&nbsp;logic:<br />//&nbsp;&nbsp;&nbsp;OTHERWISE,<br /></span><span style="color: #007700">}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;procedure:&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;show&nbsp;a&nbsp;random&nbsp;comment.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$model&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getComment</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">NULL&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$view&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">prepareComment</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$model&nbsp;</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;procedure:<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$view</span><span style="color: #007700">;</span>
</span>
</code></code></div>
</div>You'll notice that there's not a lot of code inside each <code style="background-color: #FFFFBB">if{}</code> and <code style="background-color: #FFFFBB">else{}</code> block - just a few calls to functions.  This is done on purpose.  While a coder's first thought might be to put all of the instructions right inside the block where they're being used, you'll end up with lots of nested blocks that way - it quickly becomes difficult to keep track of your Controller's logic, and mistakes start showing up.  Just like we're separating the PHP from its output, we'll separate the Model and View from their Controller.  This has the side-effect of making the code reusable (instead of writing all the code twice, just call the function again).<br />
<br />
This pattern repeats throughout the program.  The functions used in the Controller (to create the Model and the View) contain their own Controllers:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">getComment</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$commentID</span><span style="color: #007700">=</span><span style="color: #0000BB">NULL&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;controller:&nbsp;two&nbsp;possibilities...<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;1)&nbsp;we&nbsp;want&nbsp;a&nbsp;random&nbsp;comment&nbsp;(specified&nbsp;by&nbsp;making&nbsp;$commentID=NULL).<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">is_null</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$commentID&nbsp;</span><span style="color: #007700">)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;get&nbsp;a&nbsp;random&nbsp;comment&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;we&nbsp;might&nbsp;have&nbsp;gotten&nbsp;this&nbsp;info&nbsp;from&nbsp;a&nbsp;database,&nbsp;or&nbsp;somewhere&nbsp;else&nbsp;-<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;doesn't&nbsp;really&nbsp;matter&nbsp;for&nbsp;this&nbsp;example.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;What's&nbsp;important&nbsp;is,&nbsp;if&nbsp;we&nbsp;get&nbsp;the&nbsp;info,&nbsp;we&nbsp;return&nbsp;it&nbsp;in&nbsp;an&nbsp;array:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;array(&nbsp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">,</span><span style="color: #0000BB">$author</span><span style="color: #007700">,</span><span style="color: #0000BB">$publish_date&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;2)&nbsp;we&nbsp;want&nbsp;a&nbsp;specific&nbsp;comment&nbsp;(specified&nbsp;by&nbsp;a&nbsp;non-NULL&nbsp;$commentID).<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;get&nbsp;the&nbsp;comment&nbsp;with&nbsp;commentID&nbsp;=&nbsp;$commentID&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;again,&nbsp;if&nbsp;you&nbsp;find&nbsp;the&nbsp;info,&nbsp;return&nbsp;it.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;array(&nbsp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">,</span><span style="color: #0000BB">$author</span><span style="color: #007700">,</span><span style="color: #0000BB">$publish_date&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;HA!&nbsp;you&nbsp;say.&nbsp;&nbsp;A&nbsp;third&nbsp;possibility!<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;what&nbsp;if&nbsp;we&nbsp;wanted&nbsp;$commentID,&nbsp;but&nbsp;we&nbsp;couldn't&nbsp;find&nbsp;it?<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;well,&nbsp;if&nbsp;we&nbsp;found&nbsp;it,&nbsp;then&nbsp;this&nbsp;function&nbsp;has&nbsp;already&nbsp;returned&nbsp;(finished).<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;if&nbsp;not,&nbsp;then&nbsp;this&nbsp;last&nbsp;bit&nbsp;of&nbsp;code&nbsp;will&nbsp;be&nbsp;executed:<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;&nbsp;&nbsp;...and&nbsp;will&nbsp;indicate&nbsp;that&nbsp;we&nbsp;didn't&nbsp;get&nbsp;what&nbsp;we&nbsp;wanted.<br /></span><span style="color: #007700">}<br /><br />function&nbsp;</span><span style="color: #0000BB">prepareComment</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$model</span><span style="color: #007700">=</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;controller:&nbsp;two&nbsp;possibilities...<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;1)&nbsp;$model&nbsp;is&nbsp;an&nbsp;array,&nbsp;returned&nbsp;from&nbsp;getComment().<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$model&nbsp;</span><span style="color: #007700">)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;get&nbsp;the&nbsp;info&nbsp;from&nbsp;the&nbsp;Model<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">list(&nbsp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">,</span><span style="color: #0000BB">$author</span><span style="color: #007700">,</span><span style="color: #0000BB">$publish_date&nbsp;</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$model</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;use&nbsp;it&nbsp;to&nbsp;create&nbsp;the&nbsp;view<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$comment&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;&nbsp;HTML<br /></span><span style="color: #DD0000">&nbsp;&nbsp;&nbsp;&nbsp;&lt;article&nbsp;class="comment"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;</span><span style="color: #0000BB">$message</span><span style="color: #DD0000">&lt;/p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&nbsp;class="author"&gt;Published&nbsp;by&nbsp;</span><span style="color: #0000BB">$author</span><span style="color: #DD0000">&nbsp;on&nbsp;</span><span style="color: #0000BB">$publish_date</span><span style="color: #DD0000">.&lt;/p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/article&gt;<br /></span><span style="color: #007700">HTML<br />;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;and&nbsp;return&nbsp;the&nbsp;view&nbsp;(for&nbsp;the&nbsp;controller&nbsp;to&nbsp;print&nbsp;later).<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$view</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;2)&nbsp;$model&nbsp;is&nbsp;not&nbsp;an&nbsp;array&nbsp;(should&nbsp;be&nbsp;FALSE,&nbsp;indicating&nbsp;the&nbsp;comment&nbsp;wasn't&nbsp;found)<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;return&nbsp;an&nbsp;error&nbsp;message.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;&lt;&lt;&lt;&nbsp;HTML<br /></span><span style="color: #DD0000">&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&nbsp;class="error"&gt;I'm&nbsp;sorry,&nbsp;the&nbsp;monkeys&nbsp;couldn't&nbsp;find&nbsp;that&nbsp;comment.&lt;/p&gt;<br /></span><span style="color: #007700">HTML<br />;<br />}</span>
</span>
</code></code></div>
</div><br />
<i><font size="4">Hint, Hint, Hint...</font></i><br />
<br />
You might see some possible complications in the code above: there may be some cases where errors would creep in and break things.  But error handling is a topic for another day...  :)<br />
<br />
PAQ (Please Ask Questions),<br />
- Adrian</blockquote>

]]></content:encoded>
			<dc:creator>traq</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?268-Taking-Control</guid>
		</item>
		<item>
			<title>Every PHP Tutorial Is Wrong</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?267-Every-PHP-Tutorial-Is-Wrong</link>
			<pubDate>Fri, 02 Nov 2012 23:24:41 GMT</pubDate>
			<description><![CDATA[Many PHP tutorials begin with an introduction to the "basics" of the language: the <?php ?> tags, and usually the echo construct.  For example, something like this: 
 
     
PHP: 
--------- 
<html> 
<head> 
    <title>My First PHP Page</title> 
</head> 
<body>]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Many PHP tutorials begin with an introduction to the &quot;basics&quot; of the language: the <code style="background-color: #FFFFBB">&lt;?php ?&gt;</code> tags, and usually the <code style="background-color: #FFFFBB">echo</code> construct.  For example, something like this:<br />
<br />
    <div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:144px;"><code><code><span style="color: #000000">
&lt;html&gt;<br />&lt;head&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;My&nbsp;First&nbsp;PHP&nbsp;Page&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World!"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span>&lt;/body&gt;<br />&lt;/html&gt;</span>
</code></code></div>
</div>Before I blast this, I want to make clear that it's definitely not one of the worst examples out there.  It actually parses!  Additionally, the reason this is &quot;Wrong&quot; has nothing to do with the example itself, but with how it is presented.<br />
<br />
<font size="4"><font color="#696969">Hello, World!</font></font><br />
<br />
PHP is very welcoming to new coders because of the flexibility it allows in writing code, and, more importantly, in integrating that code with your HTML.  Just open PHP (<code style="background-color: #FFFFBB">&lt;?php</code>), write your code, and close PHP (<code style="background-color: #FFFFBB">?&gt;</code>) to get back to your HTML page.  A nice, simple example of a dynamic page:<br />
<br />
    <div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:336px;"><code><code><span style="color: #000000">
&lt;!doctype&nbsp;html&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;charset="UTF-8"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Simple,&nbsp;Dynamic&nbsp;PHP&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;form&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&gt;What's&nbsp;your&nbsp;name?&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;name="visitor_name"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/label&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="submit"&nbsp;value="Tell&nbsp;Us!"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;<br /><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;!empty(&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$visitor_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$visitor_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Guest'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;Welcome&nbsp;to&nbsp;my&nbsp;Simple,&nbsp;Dynamic&nbsp;Website,&nbsp;</span><span style="color: #0000BB">$visitor_name</span><span style="color: #DD0000">!&lt;/p&gt;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;/body&gt;<br />&lt;/html&gt;</span>
</code></code></div>
</div>...and everything works great.<br />
<br />
The next fancy thing you might do is try to <i>remember</i> the visitor's name during their entire visit:<br />
<br />
    <div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
&lt;!doctype&nbsp;html&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;charset="UTF-8"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Simple,&nbsp;Dynamic&nbsp;PHP&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;form&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&gt;What's&nbsp;your&nbsp;name?&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;name="visitor_name"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/label&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="submit"&nbsp;value="Tell&nbsp;Us!"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;<br /><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;session_start</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;!empty(&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$visitor_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;=&nbsp;</span><span style="color: #0000BB">$visitor_name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}elseif(&nbsp;!empty(&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$visitor_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$visitor_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Guest'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;Welcome&nbsp;to&nbsp;my&nbsp;Simple,&nbsp;Dynamic&nbsp;Website,&nbsp;</span><span style="color: #0000BB">$visitor_name</span><span style="color: #DD0000">!&lt;/p&gt;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;/body&gt;<br />&lt;/html&gt;</span>
</code></code></div>
</div>Looks great, right?  You didn't change anything, just added a few lines to save the visitor's name to the PHP session.  However, when you run this new script, you get a broken page (maybe with a warning):<br />
<div class="bbcode_container">
	<div class="bbcode_quote">
		<div class="quote_container">
			<div class="bbcode_quote_container"></div>
			
				<div class="bbcode_postedby">
					<img src="images/misc/quote_icon.png" alt="Quote" /> Originally Posted by <strong>PHP</strong>
					
				</div>
				<div class="message"><b>Warning: session_start() [function.session-start]: &quot;Cannot send session cookie - headers already sent by omg What does this even mean My SCRIPT IS BROKEN whatamigoingtodo omgomgomg type in <a href="http://www.dynamicdrive.com" target="_blank">www.dynamicdrive.com</a> NEED HLP PLZ PHP BROKE URGENT!!!?!!!!!!</b></div>
			
		</div>
	</div>
</div>You didn't even finish reading that, did you?  <font color="#A9A9A9">(You should, and for <i>so</i> many reasons.)</font><br />
<br />
So, what happened?<br />
<br />
You wrote this in a straightforward, logical order:<br />
<ul><li style="">  DOCTYPE and HTML &lt;head&gt;</li><li style="">  The &lt;form&gt; to get the visitor's name</li><li style="">  The PHP stuff to write the welcome message</li><li style="">  The closing HTML tags</li></ul><br />
<br />
Yes, that's the order that the HTML page goes in, but it's <i>not</i> the order that things actually happen in:<br />
<ol class="decimal"><li style="">  PHP script executes, <i>on your server</i></li><li style="">  PHP sends the script output <i>to the visitor's browser</i></li></ol><br />
This hints at T<font size="1">HE</font> F<font size="1">UNDAMENTAL</font> C<font size="1">HANGE</font> you made to your webpage, probably without realizing it:<br />
<br />
<font color="#fafafa">.........</font><i>This Is No Longer HTML.  <b>None Of It</b>.</i><br />
<font color="#fafafa">.........</font><i>You</i> are writing <i>PHP</i>.<br />
<font color="#fafafa">.........</font><i>PHP</i> is writing <i>HTML</i>.<br />
<br />
It's not a webpage any longer; it's a program.  It <i>makes</i> a webpage.  Even if you simply change the extension on your webpage to <code style="background-color: #FFFFBB">.php</code>, without adding one opening <code style="background-color: #FFFFBB">&lt;?php</code> tag anywhere, it's now a program, not a webpage.  Things now happen in PHP order, not HTML order.  Even when you &quot;close&quot; PHP by using the <code style="background-color: #FFFFBB">?&gt;</code> tag, PHP doesn't really &quot;close&quot;: it just takes everything you wrote, up to the next <code style="background-color: #FFFFBB">&lt;?php</code> tag, and treats it as if you'd written <code style="background-color: #FFFFBB">echo &quot;all that stuff&quot;;</code>.<br />
<br />
What's the solution?  A lot of new coders struggle with this (or similar) problems, sorting things out by trial and error.  Even many coders who make their living as Web Designers, and have a few years' experience with PHP, never really figured out what the solution was: they just built habits that happened to avoid most problems.  When the problem surfaces again, they'll still be stumped by it.<br />
<br />
<font size="4"><font color="#696969">Separation of Concerns</font></font><br />
<br />
The solution is, for lack of a simpler term, to do things &quot;in PHP order.&quot;  If you can untangle your PHP from your HTML, your program will be easier to understand and less error-prone.  The big, bad programming world calls this &quot;Separation of Concerns&quot; - meaning, simply, that your code shouldn't be tangled up.  There's a lot more to it than this, but to start with, just know:<br />
<br />
<ol class="decimal"><li style="">  <i>All</i> of your PHP code, programming logic, functions, variables, whatever, need to come first.</li><li style="">  <i>All</i> of your output needs to go last.</li></ol><br />
<br />
There are several (good) ways to rewrite those earlier examples:<br />
<br />
    <div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:156px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #FF8000">//&nbsp;PHP&nbsp;first!<br /></span><span style="color: #0000BB">$message&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;html&gt;<br />&lt;head&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;My&nbsp;First&nbsp;PHP&nbsp;Page&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000BB">&lt;?=&nbsp;$message&nbsp;?&gt;<br /></span>&lt;/body&gt;<br />&lt;/html&gt;</span>
</code></code></div>
</div>This treats the HTML part of the page as a &quot;template.&quot;  The only processing we're doing is creating a message.  The <code style="background-color: #FFFFBB">&lt;?=</code> tag <font color="#808080">(available always since version 5.4; only if `short_tags` are turned on in earlier versions)</font> is specifically for templating: it means <code style="background-color: #FFFFBB">&lt;?php echo $variable; ?&gt;</code>.  <br />
<br />
It may seem like unnecessary extra work, but we've now separated our <i>program</i> from its <i>output</i> - this is only a bit more complicated to start, but prevents a lot of problems from ever showing at all up later on.  So, do you prefer:<br />
<ol class="decimal"><li style="">  really easy now =&gt; more confusion later</li><li style="">  fairly easy now =&gt; less confusion later</li></ol><br />
?<br />
<br />
But wait; here's an even better way:<br />
<br />
    <div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:204px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #FF8000">//&nbsp;PHP&nbsp;first!<br /></span><span style="color: #0000BB">$message&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$HTML&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"<br />&lt;html&gt;<br />&lt;head&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;My&nbsp;First&nbsp;PHP&nbsp;Page&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$message</span><span style="color: #DD0000"><br />&lt;/body&gt;<br />&lt;/html&gt;"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;all&nbsp;done<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$HTML</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></code></div>
</div>Now, there's only one point where there's <i>any</i> output, and it's at the very end.  There's no wishy-washy-switching-back-and-forth between <code style="background-color: #FFFFBB">&lt;?php</code> and <code style="background-color: #FFFFBB">&lt;html&gt;</code> at all.  More importantly, <i>you understand</i> what you're doing just a little bit better.<br />
<br />
If your program:<ul><li style="">has <code style="background-color: #FFFFBB">&lt;?php</code> at the very top,</li><li style="">has only one part that generates output,</li><li style="">that part is at the very end, and</li><li style="">you can write <code style="background-color: #FFFFBB">exit;</code> afterwards without affecting anything</li></ul><br />
<br />
...you've got the makings of a pretty good program.  <font color="#A9A9A9">(Assuming, of course, it also throws no fatal errors.)</font>  As a bonus, it won't cause as many problems when you start to make your programs more complex.<br />
<br />
<font size="4"><font color="#696969">Speaking Of Which</font></font><br />
<br />
    <div class="bbcode_container">
	<div class="bbcode_description">PHP Code:</div>
	<div class="bbcode_code"style="height:372px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;it&nbsp;works&nbsp;up&nbsp;here<br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;let's&nbsp;get&nbsp;the&nbsp;visitor&nbsp;name&nbsp;from&nbsp;a&nbsp;function&nbsp;(see&nbsp;bottom&nbsp;of&nbsp;script)<br /></span><span style="color: #0000BB">$visitor_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">get_visitor_name</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;build&nbsp;our&nbsp;HTML<br />//&nbsp;this&nbsp;type&nbsp;of&nbsp;string&nbsp;definition&nbsp;is&nbsp;called&nbsp;a&nbsp;"heredoc"&nbsp;-&nbsp;<br />//&nbsp;&nbsp;&nbsp;it's&nbsp;very&nbsp;useful&nbsp;for&nbsp;writing&nbsp;large&nbsp;chunks&nbsp;of&nbsp;HTML&nbsp;because&nbsp;you&nbsp;can&nbsp;use&nbsp;quotes&nbsp;freely.<br />//&nbsp;a&nbsp;heredoc&nbsp;starts&nbsp;with&nbsp;three&nbsp;less-than&nbsp;(&lt;)&nbsp;brackets,&nbsp;a&nbsp;space,&nbsp;and&nbsp;an&nbsp;opening&nbsp;token:<br /></span><span style="color: #0000BB">$HTML&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;&nbsp;HTML<br /></span><span style="color: #DD0000">&lt;!doctype&nbsp;html&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;charset="UTF-8"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Simple,&nbsp;Dynamic&nbsp;PHP&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;form&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&gt;What's&nbsp;your&nbsp;name?&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;name="visitor_name"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/label&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="submit"&nbsp;value="Tell&nbsp;Us!"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/p&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;Welcome&nbsp;to&nbsp;my&nbsp;Simple,&nbsp;Dynamic&nbsp;Website,&nbsp;</span><span style="color: #0000BB">$visitor_name</span><span style="color: #DD0000">!&lt;/p&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /></span><span style="color: #007700">HTML<br />;&nbsp;</span><span style="color: #FF8000">//&nbsp;a&nbsp;heredoc&nbsp;ends&nbsp;with&nbsp;a&nbsp;matching&nbsp;token,&nbsp;followed&nbsp;by&nbsp;a&nbsp;semicolon.<br /><br /><br />//&nbsp;all&nbsp;done<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$HTML</span><span style="color: #007700">;<br />exit;&nbsp;</span><span style="color: #FF8000">//&nbsp;works!<br /><br />//&nbsp;define&nbsp;a&nbsp;function&nbsp;that&nbsp;gets&nbsp;the&nbsp;visitor's&nbsp;name<br />//&nbsp;&nbsp;&nbsp;(function&nbsp;definitions&nbsp;are&nbsp;"okay"&nbsp;after&nbsp;output):<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">get_visitor_name</span><span style="color: #007700">(){<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;!empty(&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$visitor_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;=&nbsp;</span><span style="color: #0000BB">$visitor_name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}elseif(&nbsp;!empty(&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;&nbsp;)&nbsp;){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$visitor_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'visitor_name'</span><span style="color: #007700">&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$visitor_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Guest'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$visitor_name</span><span style="color: #007700">;<br />}</span>
</span>
</code></code></div>
</div><font size="4"><font color="#696969">Questions?  Ask.  Late.</font></font><br />
<br />
- Adrian</blockquote>

]]></content:encoded>
			<dc:creator>traq</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?267-Every-PHP-Tutorial-Is-Wrong</guid>
		</item>
		<item>
			<title>Putting text on top of a video</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?266-Putting-text-on-top-of-a-video</link>
			<pubDate>Sun, 14 Oct 2012 17:45:06 GMT</pubDate>
			<description><![CDATA[In July 2010 an enhancement to the YouTube video embed capability became available through a new embed code style. This style uses <iframe> and looks like this: 
<iframe src="http://www.youtube.com/embed/VIDEO_ID?" frameborder="0"></iframe> 
 
 
After VIDEO_ID? we can add the normal parameters,...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">In July 2010 an enhancement to the YouTube video embed capability became available through a new embed code style. This style uses &lt;iframe&gt; and looks like this:<br />
<code style="background-color: #FFFFBB"><div style="margin-left:40px"><i>&lt;iframe src=&quot;http://www.youtube.com/embed/VIDEO_ID?&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;</i></div></code><br />
<br />
After VIDEO_ID? we can add the normal parameters, like this:<br />
<code style="background-color: #FFFFBB"><i><div style="margin-left:40px">&lt;iframe src=&quot;http://www.youtube.com/embed/VIDEO_ID?start=0&amp;amp;autoplay=1&amp;amp;rel=0&amp;amp;showinfo=0&amp;amp;autohide=0&amp;amp;modestbranding=1&amp;amp;vq=large&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;</div></i></code><br />
and also a new parameter that only works for the new embed code style:<br />
<div style="margin-left:40px"><i>&amp;amp;wmode=opaque</i> (or: <i>wmode=transparent</i>, or: <i>wmode=window</i>).</div><br />
If we put <i>wmode=opaque</i> or<i> wmode=transparent</i>, the iframe will be transparent, allowing us to put text on it (if we use the right zIndex). The iframe will not be transparent if we choose <i>wmode=window</i>.<br />
<br />
Now, putting text on a video makes only sense if we provide the text with some background (color) and font color, since the videos don't always have the same background / font color. Not using a background and a font color for the info we want to put on the video could then result in text being invisible.<br />
<br />
So we should use a background (color) and a font color for the text we put on the video. But then some part of the video gets hidden behind the text. That may provoke certain undesired results, like the top of a video being cut off.<br />
<br />
The conclusion is that, at least in certain cases, it seems better not to use the wmode parameter at all. There are other means for adding info to a video, like putting the video (vertically) under a div containing the video info and then wrapping the info div plus the iframe in another div, like this (just an example):<br />
<code style="background-color: #FFFFBB"><i><div style="margin-left:40px">&lt;div style=&quot;position: relative; top: 40px; margin: auto; width: <font color="#FF0000">600px</font>; height: <font color="#0000FF">283px</font>; overflow: <b>hidden</b>; &quot;&gt;<br />
&lt;div style=&quot;position:<b> relative</b>; text-align: center; <font color="#0000FF">height:60px; padding: 3px</font>; overflow: <b>auto</b>; background: <b>black</b>; color: <b>white</b>; font-family: verdana; font-size: 11px&quot;&gt;Text about video here&lt;br&gt;Text about video here&lt;br&gt;Text about video here&lt;br&gt;Text about video here&lt;/div&gt;<br />
&lt;iframe style=&quot;width:<font color="#FF0000"> 600px</font>; height: <font color="#0000FF">220px</font>&quot; src=&quot;http://www.youtube.com/embed/mv4cx3C3SZ4?start=0&amp;amp;autoplay=1&amp;amp;rel=0&amp;amp;showinfo=0&amp;amp;autohide=0&amp;amp;modestbranding=1&amp;amp;vq=large&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;<br />
&lt;/div&gt;</div></i></code><br />
(The (outermost) container div may have absolute or relative position; the container div must have 'overflow: hidden'; the height of the container divs equals the sum of the iframe's height and the height and padding for the info div; the div with the info must have relative position, 'overflow: auto', a background color and a font color; the container div and the iframe must have the same width; the iframe may have its own individual height).<br />
<br />
If we use <a href="http://www.dynamicdrive.com/forums/entry.php?47-Stretching-an-iframe-or-an-object-with-pixel-precision&amp;bt=281#comment281" target="_blank"><b><u>this method</u></b></a> for putting an iframe on our page, we would do something like this:<br />
<code style="background-color: #FFFFBB"><i><div style="margin-left:40px">&lt;div style=&quot;<b>position: absolute; left:100px; top: 100px; right: 100px; bottom: 100px</b>; &quot;&gt;<br />
&lt;div style=&quot;position: <b>relative</b>; text-align: center; <font color="#0000FF">height: 60px; padding: 3px</font>; overflow: <b>auto</b>; background: <b>black</b>; color: <b>white</b>; font-family: verdana; font-size: 11px&quot;&gt;Text about video here&lt;br&gt;Text about video here&lt;br&gt;Text about video here&lt;br&gt;Text about video here&lt;br&gt;Text about video here&lt;/div&gt;<br />
&lt;div style=&quot;<b>position: absolute; left:0px; top: <font color="#0000FF">63px</font>; right:0px; bottom: 0px</b>&quot;&gt;<br />
&lt;iframe style=&quot;<b>position: absolute; width: 100%; height: 100%</b>&quot; src=&quot;http://www.youtube.com/embed/mv4cx3C3SZ4?start=0&amp;amp;autoplay=1&amp;amp;rel=0&amp;amp;showinfo=0&amp;amp;autohide=0&amp;amp;modestbranding=1&amp;amp;vq=large&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</div></i><br />
</code><br />
where the height for the div (directly) containing the iframe equals the sum of the height and the padding for the text of the info div.<br />
<br />
Copy the examples above, paste them in a file and experiment with the result.<br />
<br />
Arie.</blockquote>

]]></content:encoded>
			<dc:creator>molendijk</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?266-Putting-text-on-top-of-a-video</guid>
		</item>
		<item>
			<title>FaceScroll - my take</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?264-FaceScroll-my-take</link>
			<pubDate>Sat, 13 Oct 2012 20:32:39 GMT</pubDate>
			<description><![CDATA[Adds altScrollTo method to scroll to coordinates, element, or keyword position in the scroller. Adds optional callback for real time updates of the scroller's position. Fixes behavior of bi-directional scrollers in IE 8 and less. Now compatible to and including IE 6. Adds automatic console logging...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Adds altScrollTo method to scroll to coordinates, element, or keyword position in the scroller. Adds optional callback for real time updates of the scroller's position. Fixes behavior of bi-directional scrollers in IE 8 and less. Now compatible to and including IE 6. Adds automatic console logging of missing helper scripts in browsers with console logging. Instates selector element return value for normal jQuery chaining of methods. Alters the mouseout behavior for default style scrollers to avoid jumpiness when still dragging a scrollbar.<br />
<br />
Applies to:<br />
<br />
<a href="http://www.dynamicdrive.com/dynamicindex11/facescroll/index.htm" target="_blank">FaceScroll Custom scrollbar</a><br />
<br />
This is a dropin replacement for FaceScroll Custom scrollbar that enables an altScrollTo method for scrolling to any given point within a scroller via top and/or horizontal coordinates, or via an element with an id within a scroller, or via the keywords 'begin', 'middle', or 'end' to scroll to those positions respectively.<br />
<br />
It also adds an option for real time updates of the scroller's postion for use as you see fit.<br />
<br />
It corrects the scroller's behavior in IE 8 and less down to and including IE 6, especially IE 7 and 6 which were previously excluded form this script. IE 8 only had problems with bi-directional scrollers. And adds the previously missing common jQuery object return value for its primary function so that it may be chained from in the normal manner that users of jQuery are accustomed to.<br />
<br />
For non-touch screen browsers it changes the behavior for default style scrollers, the ones where the scrollbars are hidden on mouseout. If the user is still dragging the scrollbar on mouseout, it will remain visible and functional until the mouseup event that releases it. In the original version it would disappear and the scroller could jump or do other odd things depending upon the browser.<br />
<br />
Demo:<br />
<br />
<a href="http://home.comcast.net/~jscheuer1/side/facescroll/demo-2.htm" target="_blank">http://home.comcast.net/~jscheuer1/s...oll/demo-2.htm</a><br />
<br />
You may use your browser's view source to see the syntax used in the demo, which is the same as the original, just with the added option and function.<br />
<br />
Script (right click and 'Save As', requires the same helper scripts as does the original version):<br />
<br />
<a href="http://home.comcast.net/~jscheuer1/side/facescroll/facescroll.js" target="_blank">http://home.comcast.net/~jscheuer1/s.../facescroll.js</a><br />
<br />
From the release notes in the updated script:<br />
<br />
&quot;Allows for an optional scrolldatato(data) function that returns the scroll<br />
position in real time vertically and horizontally of that scroller's content. It's data argument returns<br />
an object with that faceScroll scroller's top and left positions as negative floating point numbers.<br />
Example syntax for using scrolldatato(data) which is used as an option during initialization:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:84px;">	$('#demo1').alternateScroll({'scrolldatato': function(data){
			$('#dat-top').val(Math.abs(Math.round(data.top)));
			$('#dat-left').val(Math.abs(Math.round(data.left)));
		}
	});</pre>
</div>The above example will populate the text inputs with id's of 'dat-top' and 'dat-left' with the $('#demo1')<br />
FaceScroll scroller's top and left scroll positions respectively. These will be updated in real time as<br />
the scroller scrolls. Using the Math.abs and Math.round methods will translate the negative floating<br />
point values to positive whole integers.<br />
<br />
Adds an an altScrollTo() function as a jQuery method to scroll vertically<br />
and/or horizontally to a positive integer pixel destination or to a jQuery selector of an element within<br />
the content. The altScrollTo() function returns its selector object and the alternateScroll() function<br />
now does so too. These are in accordance with jQuery convention to allow for chaining.<br />
The available syntaxes for altScrollTo() are:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:252px;">	$('#selector').altScrollTo(80); // will scroll the FaceScroll scroller initialized on $('#selector')
		// to 80 pixels vertical, unless the scroller only scrolls horizontally, in which case it will
		// scroll to 80 pixels horizontal.

	$('#selector').altScrollTo(80, 50); // will scroll the FaceScroll scroller initialized on $('#selector')
		// to 80 pixels vertical, 50 pixels horizontal, unless that scroller scrolls only horizontally,
		// in which case it will only scroll to 50 pixels horizontal, or unless that scroller only scrolls
		// vertically, in which case it will only scroll to 80 pixels vertical.

	$('#selector').altScrollTo('#selector2'); // will scroll the FaceScroll scroller initialized on $('#selector')
		// to the element within it with the id of 'selector2'.

	$('#selector2').altScrollTo(); // a shortcut method for $('#selector').altScrollTo('#selector2'); will scroll
		// the FaceScroll scroller that contains the element that has the id of 'selector2' to that element.
		// Be careful here, if two scrollers contain the same selector, unexpected results may occur.

	$('#selector').altScrollTo('end'); // will scroll the FaceScroll scroller initialized on $('#selector')
		// to the end both vertically and horizontally, or only one or the other if that scroller only
		// scrolls in that one direction. Other available keywords are 'middle', and 'begin'.</pre>
</div>If as can be done, multiple scrollers were initialized under one selector, one may use either a selector<br />
for the specific scroller one wants to scroll, or the shortcut method which will find the scroller that<br />
contains that id selector in it's content.<br />
<br />
For non-touch screens improves behavior on mouseout while still scrolling a Facescroll element with 'hide-bars': true&quot;<br />
<br />
<br />
Comments are welcome but will be moderated until approved.<br />
<br />
If you want help with this though, please post a new thread in the Dynamic Drive Scripts help section. Link to this blog entry in your post.</blockquote>

]]></content:encoded>
			<dc:creator>jscheuer1</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?264-FaceScroll-my-take</guid>
		</item>
		<item>
			<title>Scroll to Top Script</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?265-Scroll-to-Top-Script</link>
			<pubDate>Thu, 11 Oct 2012 01:00:34 GMT</pubDate>
			<description><![CDATA[Here's my alternative to the ddscript Jquery Scroll to Top Control V1.1 (http://www.dynamicdrive.com/dynamicindex3/scrolltop.htm) 
 
*Attachment 4800 (http://www.dynamicdrive.com/forums/attachment.php?attachmentid=4800)* 
 
 
Code: 
--------- 
$.fn.scroll_to_top = function(userOptions) { 
	var...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Here's my alternative to the ddscript <a href="http://www.dynamicdrive.com/dynamicindex3/scrolltop.htm" target="_blank">Jquery Scroll to Top Control V1.1</a><br />
<br />
<b><a href="http://www.dynamicdrive.com/forums/attachment.php?attachmentid=4800&amp;d=1349917057"  title="Name:  scroll_to_top.js
Views: 492
Size:  2.0 KB">scroll_to_top.js</a></b><br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:372px;">$.fn.scroll_to_top = function(userOptions) {
	var check_scroll = 0;
    var defaultOptions = {
		image : 'jump_to_top.png',
		image_height : '6%',
		image_width : '3%',
		scroll_distance : 200,
		scroll_location : 0,
		scroll_speed : 600,
		button_animate_speed : 200,
		button_animate_type : 'fade',
		position : 'bottom_left'
    };
    var args = defaultOptions;
    if(typeof userOptions === 'object') { 
		args = $.extend(defaultOptions,userOptions);
	}
	switch(args.position) {
		case 'top_left':
			var css = 'top:0;left:0;';
		break;
		case 'top_right':
			var css = 'top:0;right:0;';
		break;
		case 'bottom_right':
			var css = 'bottom:0;right:0;';
		break;
		case 'center':
			var css = 'top:50%;bottom:50%;left:50%;right:50%;';
		break;
		default:
			var css = 'bottom:0;left:0;';
	}
	css += 'margin-bottom:5px;margin-top:5px;margin-left:5px;margin-right:5px;display:none;position:fixed;height:' + args.image_height + ';width:' + args.image_width + ';cursor:pointer;';
	$('body').append('&lt;img src=&quot;' + args.image + '&quot; alt=&quot;Scroll to top&quot; title=&quot;Scroll to top&quot; id=&quot;scroll_to_top&quot; style=&quot;' + css + '&quot;/&gt;');
	$(window).scroll(function() {
		if($(this).scrollTop() &gt; args.scroll_distance) {
			switch(args.button_animate_type) {
				case 'slide' : 
					$('#scroll_to_top').slideDown(args.button_animate_speed);
				break;
				default:
					$('#scroll_to_top').fadeIn(args.button_animate_speed);
			}
        } else {
			switch(args.button_animate_type) {
				case 'slide' : 
					$('#scroll_to_top').slideUp(args.button_animate_speed);
				break;
				default:
					$('#scroll_to_top').fadeOut(args.button_animate_speed);
			}
        }
		if(check_scroll === 1) {
			if($(this).scrollTop() === 0) {
				if(typeof args.success != &quot;undefined&quot;) {
					args.success();
					check_scroll = 0;
				}
			}
		}
	});
	$('#scroll_to_top').click(function() {
		$('body,html').animate({
			scrollTop : args.scroll_location
		}, args.scroll_speed);
		check_scroll = 1;
	});
};</pre>
</div>To call the script, put this code in the &lt;head&gt;&lt;/head&gt; of your body -<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:264px;">&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;scroll_to_top.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(window).load(function(){ 
	$('body').scroll_to_top({
		image : &quot;jump_to_top.png&quot;,
		image_height : &quot;6%&quot;,
		image_width : &quot;3%&quot;,
		scroll_distance : 200,
		scroll_speed : 600,
		//scroll_location : 50,
		button_animate_speed : 200,
		button_animate_type : 'fade',
		position : 'bottom_left',
		success : function() {
			//alert(&quot;Complete&quot;);
		}
	});
});
&lt;/script&gt;</pre>
</div>Just edit the settings as needed.<br />
If you do not pass one of the settings to the script, the default settings will kick in.<br />
<br />
Here's the image used, but you can substitute your own - <a href="http://www.dynamicdrive.com/forums/attachment.php?attachmentid=4790&amp;d=1349917057"  title="Name:  scroll_to_top.js
Views: 492
Size:  2.0 KB">Attachment 4790</a><br />
<br />
<a href="http://jsfiddle.net/mEGkF/" target="_blank">Here's</a> a working example.</blockquote>

]]></content:encoded>
			<dc:creator>keyboard1333</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?265-Scroll-to-Top-Script</guid>
		</item>
		<item>
			<title>Crawler Interim Updates</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?260-Crawler-Interim-Updates</link>
			<pubDate>Mon, 08 Oct 2012 15:39:28 GMT</pubDate>
			<description><![CDATA[Adds: persistence option, oncontentinit option, auto detect of a single table with a single row for optionally randomizing its td's, code efficiencies. 
 
Applies to: 
 
Text and Image Crawler v1.5 (http://www.dynamicdrive.com/dynamicindex2/crawler/index.htm) 
 
What many people don't know about...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Adds: persistence option, oncontentinit option, auto detect of a single table with a single row for optionally randomizing its td's, code efficiencies.<br />
<br />
Applies to:<br />
<br />
<a href="http://www.dynamicdrive.com/dynamicindex2/crawler/index.htm" target="_blank">Text and Image Crawler v1.5</a><br />
<br />
What many people don't know about Crawler is that it makes a duplicate train of its text and/or images so that they may be looped/viewed in a continuous manner without spaces or jumpiness.<br />
<br />
That's fine as long as you're not trying to initialize that content to another script like a lightbox.<br />
<br />
Some people have expressed a desire to have Crawler remember (persist) its state (position and direction) when refreshed and when the same Crawler is on different pages.<br />
<br />
This is a drop-in replacement for the current version 1.5 crawler.js file. It adds the ability to run a callback function once the crawler divisions have been populated and made a part of the DOM. Example:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:348px;">marqueeInit({
	uniqueid: 'mycrawler2',
	style: {
		'padding': '2px',
		'width': '700px',
		'height': '240px'
		
	},
	inc: 20, //speed - pixel increment for each iteration of this marquee's movement
	mouse: 'cursor driven', //mouseover behavior ('pause' 'cursor driven' or false)
	moveatleast: 3,
	neutral: 150,
	savedirection: true,
	random: false<code style="background-color: #FFFFBB">,
	initcontent: function(contentarray){
		var $ = jQuery;
		if(typeof $ !== 'undefined' &amp;&amp; typeof $.fn.fancybox === 'function'){
			$(contentarray[1]).find('a[rel=example_group]').attr('rel', 'example_group2');
			$(&quot;a[rel^=example_group]&quot;).fancybox({
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '&lt;span id=&quot;fancybox-title-over&quot;&gt;Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &amp;nbsp; ' + title : '') + '&lt;/span&gt;';
				}
			});
		}
	}</code>
});</pre>
</div>The contentarray contains two items, contentarray[0] and contentarray[1], which each contain one of the now two trains of images and/or text of the Crawler as HTML code on the page. One or both of these may be used to initialize that content to another script. The keyword <b>this</b> (not used in the above example) would refer to the Crawler instance.<br />
<br />
<br />
Persistence is easy to add. Example:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:192px;">marqueeInit({
 uniqueid: 'art',
 direction: 'right',
 style: { //style object for this marquee container (use quotes on both sides of the : as shown)
	'width': '60%',
	'height': '235px',
	'background-color': '#ddd',
	'border': '1px solid #111',
	'margin': '2ex auto 0 auto'},
 inc: 6, //speed - pixel increment for each iteration of this marquee's movement
 moveatleast: 4,
<code style="background-color: #FFFFBB"> persist: true,</code>
 mouse: 'cursor driven' //mouseover behavior ('pause' 'cursor driven' or false)
});</pre>
</div>Here's the updated script (right click and 'Save As'):<br />
<br />
<a href="http://www.dynamicdrive.com/forums/attachment.php?attachmentid=4799&amp;d=1349777832"  title="Name:  crawler.js
Views: 250
Size:  13.9 KB">crawler.js</a><br />
<br />
It also enhances the script's ability to run in Quirks Mode in IE. And if you choose to make your Crawler's content a single table with a single row, and choose the random option, it will randomize the td's.<br />
<br />
Comments are welcome, but will be held back until I determine they're not spam.<br />
<br />
If you want help with this code, please post a new thread in the Dynamic Drive Scripts Help section:<br />
<br />
<a href="http://www.dynamicdrive.com/forums/forumdisplay.php?2-Dynamic-Drive-scripts-help" target="_blank">http://www.dynamicdrive.com/forums/f...e-scripts-help</a><br />
<br />
and post a link to this Blog entry in your message. Use the word Crawler in your title.<br />
<br />
<div style="border:1px solid red; width: 95%; padding: 5px;"><b>Edit:</b> <i>Oct/9/12 added auto detect table, code efficiencies, details and updated attached script.</i></div></blockquote>

]]></content:encoded>
			<dc:creator>jscheuer1</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?260-Crawler-Interim-Updates</guid>
		</item>
		<item>
			<title><![CDATA[[jQuery-append] includes HTML as well as JS]]></title>
			<link>http://www.dynamicdrive.com/forums/entry.php?263-jQuery-append-includes-HTML-as-well-as-JS</link>
			<pubDate>Mon, 20 Aug 2012 00:07:11 GMT</pubDate>
			<description>1. 
Create a string that is the JS-equivant of the DD Anylink standalone menu (http://www.dynamicdrive.com/dynamicindex1/dropmenuindex.htm) (or of any standalone menu) with the help of a good HTML-to-JS-converter. Make sure that all JS and CSS are made inline before conversion. Name the string...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">1.<br />
Create a string that is the JS-equivant of the<a href="http://www.dynamicdrive.com/dynamicindex1/dropmenuindex.htm" target="_blank"> DD Anylink standalone menu</a> (or of any standalone menu) with the help of a good HTML-to-JS-converter. Make sure that all JS and CSS are made inline before conversion. Name the string <font color="#FF0000">included_js</font>.<br />
2.<br />
Create a script like this:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:60px;">&lt;script&gt;
var <font color="#FF0000">included_js</font> = STRING, where STRING represents the string you just created.
&lt;/script&gt;</pre>
</div>(in the Anylink-case, STRING representsw <a href="http://mesdomaines.nu/eendracht/include_jquery_style/this.txt" target="_blank">THIS</a>) and put the script in a HTML-file. Put it just after the body tag. <br />
<br />
3.<br />
Add another script (to the HTML-file), after the body tag (and after the preceding script) having this:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:60px;">&lt;script&gt;
$(&quot;body&quot;).append(<font color="#FF0000">included_js</font>)
&lt;/script&gt;</pre>
</div>Of course, the HTML-file should also have <br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:36px;">&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js&quot;&gt;&lt;/script&gt;</pre>
</div> at the top<br />
4.<br />
Open the file. You'll notice that the HTML-file shows a well functioning menu (Anylink, in our case). This shows that jQuery-append does not only bring in HTML, but also JS (and CSS).<br />
5.<br />
If you don't want the menu to show on page load, but on click, we should not have:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:60px;">&lt;script&gt;
$(&quot;body&quot;).append(<font color="#FF0000">included_js</font>)
&lt;/script&gt;</pre>
</div>but:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:60px;">&lt;script&gt;
$(&quot;body&quot;).append('&lt;div style=&quot;display: none&quot;&gt;'+<font color="#FF0000">included_js</font>+'&lt;\/div&gt;')
&lt;/script&gt;</pre>
</div>and then lines like:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:48px;">&lt;a href=&quot;javascript<b></b>: void(0)&quot; onclick=&quot;$('#jquery_include').empty(); $('#jquery_include').append(<font color="#FF0000">included_js</font>); return false&quot;&gt;append string&lt;/a&gt;
&lt;div id=&quot;jquery_include&quot; style=&quot;position: relative; width:500px;&quot;&gt;&lt;/div&gt;</pre>
</div>That's what I'have done in the <a href="http://mesdomaines.nu/eendracht/include_jquery_style/jquery_include.html" target="_blank">DEMO</a> page.<br />
5.<br />
The string can be 'emptied' by doing:<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:36px;">&lt;a href=&quot;javascript<b></b>: void(0)&quot; onclick=&quot;$('#jquery_include').empty(); return false&quot;&gt;remove string&lt;/a&gt;</pre>
</div>6.<br />
What this show is that <i>jQuery-append</i> can be forced to act like some sort of asynchronous document.write. Of course, finding an elegant way of creating STRING (other than using a HTML-to_JS-converter and without spoiling things) is another matter.<br />
===<br />
Arie.</blockquote>

]]></content:encoded>
			<dc:creator>molendijk</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?263-jQuery-append-includes-HTML-as-well-as-JS</guid>
		</item>
	</channel>
</rss>
