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

<channel>
	<title>Jonathan&#039;s Universe &#187; php</title>
	<atom:link href="http://jknipp.com/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://jknipp.com</link>
	<description>Make Yourself At Home</description>
	<lastBuildDate>Sun, 27 Jun 2010 17:39:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>PHP Datatype Validation</title>
		<link>http://jknipp.com/2009/02/06/geeky-rants/185/php-datatype-validation</link>
		<comments>http://jknipp.com/2009/02/06/geeky-rants/185/php-datatype-validation#comments</comments>
		<pubDate>Fri, 06 Feb 2009 05:23:26 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Geeky Rants]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://jknipp.com/?p=185</guid>
		<description><![CDATA[So, let&#8217;s say that you&#8217;re writing a function or some sort of script. and you need to sure you&#8217;re only getting a certain datatype as a value. Then you realize that PHP has no actual datatype enforcement like C or &#8230; <a href="http://jknipp.com/2009/02/06/geeky-rants/185/php-datatype-validation">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://jknipp.com/2008/02/29/php/9/php-powered-irc-services' rel='bookmark' title='PHP Powered IRC Services?'>PHP Powered IRC Services?</a> <small>So. For awhile, I&#8217;ve been thinking of doing something. I probably really won&#8217;t have the time to do it, but...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>So, let&#8217;s say that you&#8217;re writing a function or some sort of script. and you need to sure you&#8217;re only getting a certain datatype as a value. Then you realize that PHP has no <em>actual</em> datatype enforcement like C or C++. You can set any variable to contain any kind of structure (strings, integers, arrays, etc) and you can later change it to any other structure later on the fly. This can be problematic depending on what you need to do.</p>
<p>With the function you&#8217;re supposedly writing, let&#8217;s say you wanted to make sure a argument you&#8217;re accepting via the PHP call is an integer. Well you could perform and is_int() function call by yourself, but thats not the point. The point of functions is to reduce tedious/repeated code. I made a class that has a function inside it that does all the testing for you depending on what  you, as a coder, is expecting. So passing &#8220;hi&#8221; into the validator when you specify you&#8217;re looking for an integer wouldn&#8217;t pass. It would pass if you stated you were looking for a string, though.</p>
<p>What&#8217;s also nifty is that i wrote it to accommodate for the event of expecting multiple kinds of datatypes. Like let&#8217;s say you want either an integer or a double (a decimal), you can simple combine them when you call the validator. If the value you give the validator is an integer or a double, it&#8217;ll spit back the datatype.</p>
<p>Okay, how do I have it check for datatypes? Every datatype that I could look for is assigned a number in base 2 (1, 2, 4, 8, 16, 32, etc) . I assign them base 2 values in hex, simple because I felt like it.</p>
<blockquote><p>define(&#8220;SCRAPBOOK_DATATYPE_STR&#8221;, 0&#215;01); // STRING<br />
define(&#8220;SCRAPBOOK_DATATYPE_ARR&#8221;, 0&#215;02); // ARRAY<br />
define(&#8220;SCRAPBOOK_DATATYPE_INT&#8221;, 0&#215;04); // INTEGER<br />
define(&#8220;SCRAPBOOK_DATATYPE_DOUBLE&#8221;, 0&#215;08); // DOUBLE</p></blockquote>
<p>SCRAPBOOK_DATATYPE_STR is set to 1, _ARR is 2, etc etc. You get the point. When I call the function, the first argument/parameter is the value I need to test. The second is the (possible) datatype(s) I&#8217;m expecting. What you may or may not know about bit addition is that its very easy to figure out what bits are included. For example, the value of 6 is ONLY made up of 2 and 4, which in  this are the bits that are assigned to _ARR and _INT. Using this logic, we can pass multiple datatypes we might get (for example, we want a number which might be an integer or a decimal) and the function can check to see if the datatype of the value I&#8217;m checking for matches one of the datatypes I&#8217;m looking for.</p>
<p>Here is an example of looking for one datatype and then two datatypes:</p>
<blockquote><p>//first instantiate the object<br />
$validate = new Validator();</p>
<p>//would return SCRAPBOOK_DATATYPE_ARR<br />
print $validate-&gt;validateDatatype(array(), SCRAPBOOK_DATATYPE_ARR).&#8221;\n&#8221;;</p>
<p>//would return SCRAPBOOK_DATATYPE_INT<br />
print $validate-&gt;validateDatatype(5, SCRAPBOOK_DATATYPE_INT|SCRAPBOOK_DATATYPE_DOUBLE).&#8221;\n&#8221;;<br />
//would return SCRAPBOOK_DATATYPE_DOUBLE<br />
print $validate-&gt;validateDatatype(5.3, SCRAPBOOK_DATATYPE_INT|SCRAPBOOK_DATATYPE_DOUBLE).&#8221;\n&#8221;;</p></blockquote>
<p>As you can see, the pipe (|) is used to add multiple of these bits together. The function would return the bit of the dataype found. this way if you&#8217;re expecting two datatypes, you can validate it and then see the exact type that was actually sent in.</p>
<p>Now how do i actually do the tests? well in the function I have something like this:</p>
<blockquote><p>function validateDatatype($value = NULL, $expectedtypes = SCRAPBOOK_DATATYPE_STR)<br />
{<br />
//are we testing if this datatype exists?  ||   now we test!<br />
if ( ($expectedtypes &amp; SCRAPBOOK_DATATYPE_STR) &amp;&amp; (is_string($value)) )<br />
return SCRAPBOOK_DATATYPE_STR;<br />
if ( ($expectedtypes &amp; SCRAPBOOK_DATATYPE_ARR) &amp;&amp;  (is_array($value)) )<br />
return SCRAPBOOK_DATATYPE_ARR;<br />
if ( ($expectedtypes &amp; SCRAPBOOK_DATATYPE_INT) &amp;&amp;  (is_int($value)) )<br />
return SCRAPBOOK_DATATYPE_INT;<br />
if ( ($expectedtypes &amp; SCRAPBOOK_DATATYPE_OBJECT) &amp;&amp;  (is_object($value)) )<br />
return SCRAPBOOK_DATATYPE_OBJECT;</p></blockquote>
<p>Essentially, i&#8217;m testing to see if we&#8217;re asking for each datatype, if we are asking for that datatype, i perform a function call to see if it actually is. If it&#8217;s not it keeps going until it returns something. if  it gets to the end of the function, that means it didn&#8217;t validate, so false is returned. The (<strong>x</strong> &amp;  <strong>y</strong>) test basically checks to see if bit <strong>y</strong> exists within value <strong>x</strong>. So again,  for example, lets say you have a number 13. The only way to get a number 13 through this method is  with numbers 8, 4 and 1 (8+4+1 = 13). So if you did (13 &amp; 4) it&#8217;d return true.  So on and so forth.</p>
<p>Ok, now that I gave you little snippets, <a href="http://privatepaste.com/731Yi9AGxC" target="_blank">check out</a> the entire script so you can see how it all blends together.</p>
<p>Related posts:<ol>
<li><a href='http://jknipp.com/2008/02/29/php/9/php-powered-irc-services' rel='bookmark' title='PHP Powered IRC Services?'>PHP Powered IRC Services?</a> <small>So. For awhile, I&#8217;ve been thinking of doing something. I probably really won&#8217;t have the time to do it, but...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://jknipp.com/2009/02/06/geeky-rants/185/php-datatype-validation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Leap Seconds</title>
		<link>http://jknipp.com/2008/09/11/geeky-rants/17/leap-seconds</link>
		<comments>http://jknipp.com/2008/09/11/geeky-rants/17/leap-seconds#comments</comments>
		<pubDate>Thu, 11 Sep 2008 17:47:58 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Geeky Rants]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[leap seconds]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[time zones]]></category>
		<category><![CDATA[UTC]]></category>

		<guid isPermaLink="false">http://jknipp.com/?p=17</guid>
		<description><![CDATA[So, I noticed when my employer moved to a new web-server running (at the time) the most up-to-date version of mySQL and PHP there were some discrepancies with dates. Then I realized something. For some reason when certain functions of &#8230; <a href="http://jknipp.com/2008/09/11/geeky-rants/17/leap-seconds">Continue reading <span class="meta-nav">&#8594;</span></a><!--- 
No related posts. -->]]></description>
			<content:encoded><![CDATA[<p>So, I noticed when my employer moved to a new web-server running (at the time) the most up-to-date version of mySQL and PHP there were some discrepancies with dates.</p>
<p>Then I realized something. For some reason when certain functions of the website posted things to the database that involved time, it was off.  When the database entries did not have hours,minutes,seconds and just the date, it&#8217;d always push the date to be the previous day. This was strange and very annoying. You would post a news entry, and it&#8217;d be displayed as if it were posted the day previous. If you posted a sports schedule, the game listing would be off a day (this could cause definite issues).</p>
<p>After investigating this further, it turned out that database entries <em>with</em> hours,minutes,seconds did display the time as 23 seconds behind. Always 23 seconds. Always. This makes sense, because on date fields that don&#8217;t have the hourly time written, it&#8217;s internally notated as midnight (00:00:00).  So, if you take 23 seconds away from midnight, it&#8217;s the previous day. What could cause this? PHP (the programming language that&#8217;s calling the data from the database) and mySQL is running off the same machine with the same clock, so it&#8217;s not like it&#8217;s desync&#8217;d.</p>
<p>After even further investigation, I traced down the issue. Apparently the international time committee decided to implement <a href="http://en.wikipedia.org/wiki/Leap_seconds" target="_blank">leap seconds</a> in the <a href="http://en.wikipedia.org/wiki/UTC" target="_blank">UTC time standard</a>. These leap seconds are implemented to compensate for the gradual slowing of the earth&#8217;s rotation due to the <a href="http://en.wikipedia.org/wiki/Tidal_effect" target="_blank">tidal effect</a>. mySQL accounts for the leap seconds. PHP doesn&#8217;t. Or maybe vice versa. I don&#8217;t know. I would think if leap seconds are <em>added</em>, it&#8217;d be 23 seconds ahead. Perhaps I&#8217;m just thinking backwards about this.</p>
<p>Apparently these people don&#8217;t think of the consequences of their decisions in regards to hardware and software where time precision is required. Especially when midnight is used for the baseline in setting times. Right now I&#8217;m going to investigate the possibility of changing the mySQL timezone in favor of a &#8220;time standard&#8221; that doesn&#8217;t reflect leap seconds. Perhaps GMT-5.  instead of UTC-6. Or whatever.</p>
<p>Regardless, this just shows why UTC should not be used when consistency is required.</p>
<!--- <p>No related posts.</p> -->]]></content:encoded>
			<wfw:commentRss>http://jknipp.com/2008/09/11/geeky-rants/17/leap-seconds/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>srvx Staff Log Reviewer</title>
		<link>http://jknipp.com/2008/02/25/shameless-plugs/5/srvx-staff-log-reviewer</link>
		<comments>http://jknipp.com/2008/02/25/shameless-plugs/5/srvx-staff-log-reviewer#comments</comments>
		<pubDate>Tue, 26 Feb 2008 01:33:34 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Geeky Rants]]></category>
		<category><![CDATA[Shameless Plugs]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[srvx]]></category>
		<category><![CDATA[universium]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://jknipp.com/2008/02/25/shameless-plugs/5/srvx-staff-log-reviewer</guid>
		<description><![CDATA[So. Today I decided to stay home because I was feeling really under the weather, pretty much all weekend. But I eventually went in (like at 4, which is really late, but meh) and got some work in. Then my &#8230; <a href="http://jknipp.com/2008/02/25/shameless-plugs/5/srvx-staff-log-reviewer">Continue reading <span class="meta-nav">&#8594;</span></a><!--- 
No related posts. -->]]></description>
			<content:encoded><![CDATA[<p>So.</p>
<p>Today I decided to stay home because I was feeling really under the weather, pretty much all weekend. But I eventually went in (like at 4, which is really late, but meh) and got some work in. Then my friend, Michelle, who now works for the district too wanted to work out, so both of us went down to weight room and of course it&#8217;s locked. Can&#8217;t I just unlock it? No, of course not. I don&#8217;t even have a key to my office. And of course, there were no custodians around. I don&#8217;t even know if they would&#8217;ve let us in.  So we&#8217;re gonna try again tomorrow, and probably a lot earlier. Like.. I dunno maybe 2 or 3?</p>
<p>So, instead. We went to the pool room and just hung out with two other kids. Then she drove me to Wawa. I had to get cigarettes for my dad. He needs to quit. Don&#8217;t you think so? So, I left wawa and she drove me home. Then like.. I was like. here at my like computer. Totally.</p>
<p>Anyway.</p>
<p>I&#8217;m the network manager for Universium IRC network. It&#8217;s pretty cool. If you have an IRC client, you should probably point it to irc.universium.net and join #Universium because all the cool people are doing it. I&#8217;ll hook y&#8217;all with a java applet one of these days. Anyway.. I&#8217;ve been working on a PHP powered bot that connects to the network. The bot sits in the staff log channel and takes those logs and saves them to a database. Like so:</p>
<p><a href="http://jknipp.com/wp-content/uploads/2008/02/srv_overrides.JPG" title="srvx overrides in PHPmyadmin"><img src="http://jknipp.com/wp-content/uploads/2008/02/srv_overrides.thumbnail.JPG" alt="srvx overrides in PHPmyadmin" border="0" height="86" width="211" /></a></p>
<p>This is what the database looks like from a PHPMyAdmin viewer. I then take the data and read from it, so I can review staff actions.. This way, I can check for abuse.</p>
<p>So anyway, I can take that data and format so I can make a webpage to display the logs for review.</p>
<p><a href="http://jknipp.com/wp-content/uploads/2008/02/overrideviewer.gif" title="srvx overrides in website"><img src="http://jknipp.com/wp-content/uploads/2008/02/overrideviewer.gif" alt="srvx overrides in website" border="0" height="210" width="411" /></a></p>
<p>Then those flags change the status of the respective entry. If I click a red flag, it highlights the respective entry. This way it stands out.. and also, a flagged entry cannot be deleted until it&#8217;s unflagged (or.. green flagged).</p>
<p>It makes my life easier.. also for anyone that is hired to review the logs.</p>
<!--- <p>No related posts.</p> -->]]></content:encoded>
			<wfw:commentRss>http://jknipp.com/2008/02/25/shameless-plugs/5/srvx-staff-log-reviewer/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

