<?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>Ryan Price &#187; PHP Snippets</title>
	<atom:link href="http://www.ryanprice.ca/category/technology/php-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ryanprice.ca</link>
	<description>The definitive proof that I'm a geek at heart</description>
	<lastBuildDate>Wed, 01 Sep 2010 18:23:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to Automatically Create Getter and Setter Methods in PHP</title>
		<link>http://www.ryanprice.ca/how-to-automatically-create-getter-and-setter-methods-in-php/</link>
		<comments>http://www.ryanprice.ca/how-to-automatically-create-getter-and-setter-methods-in-php/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 22:38:33 +0000</pubDate>
		<dc:creator>Ryan Price</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Snippets]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.ryanprice.ca/work/?p=30</guid>
		<description><![CDATA[In PHP 5+, PHP has introduced a handy little function __call() that is used as a type of last resort when the function being called can not be found within the class. By utilizing this feature, we are able to write generic code that will create getter and setter methods for each attribute in your [...]<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-automatically-create-getter-and-setter-methods-in-php/">How to Automatically Create Getter and Setter Methods in PHP</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In PHP 5+, PHP has introduced a handy little function <tt>__call()</tt> that is used as a type of last resort when the function being called can not be found within the class.  By utilizing this feature, we are able to write generic code that will create getter and setter methods for each attribute in your class.<br />
<span id="more-30"></span></p>
<p>Not only does this create a getter and setter method for each attribute, but it also means that no maintenance to getter and setter methods are required when you add or remove attributes at a later point.</p>
<p>Lets take a look at a sample class called <tt>Picture</tt> which simply describes an image that is stored on the server at some URL.</p>
<pre name="code" class="php">
class Picture {

	protected $id;
	protected $type;
	protected $width;
	protected $height;
	protected $name;
	protected $url;

	public function setID($id) {
		if (!$this->id) {
			$this->id = $id;
		}
	}

	/**
	 * This serves as the default getter and setter methods for all attributes that don't expressively have one
	 *
	 * @param unknown_type $name
	 * @param unknown_type $args
	 * @return unknown nothing
	 */
	public function __call($name, $args) {
		if (preg_match('/^(get|set)(w+)/', strtolower($name), $match) &#038;&#038; $attribute = $this->validateAttribute($match[2])) {
			if ('get' == $match[1]) {
				return $this->$attribute;
			} else {
				$this->$attribute = $args[0];
			}
		} else {
			throw new Exception('Call to undefined method Bookmark::'.$name.'()');
		}
	}

	/**
	 * Validates that the attribute is part of the class
	 *
	 * @param unknown_type $name name of the attribute
	 * @return unknown returns the name of the attribute
	 */
	protected function validateAttribute($name) {
		if (in_array(strtolower($name), array_keys(get_class_vars(get_class($this))))) {
			return strtolower($name);
		}
	}
}
</pre>
<p>The code above in <tt>__call()</tt> and <tt>validateAttribute</tt> is used as the getter and setter functions for any attribute <em>which does not already have a getter or setter defined</em>.  This is important to note.  <tt>__call()</tt> is only used as a last resort.  Therefore, if you want to create a getter or setter that behaves differently then normal it is not a problem.  In fact, that is exactly what I have done for <tt>setID</tt>.</p>
<p>In this case, the ID is not something we want to be overridden once it is set.  In my case, the purpose of this ID was to identify the picture&#8217;s ID in the database.  So once the ID is set (likely when the object is created), the ID can no longer be changed.</p>
<p>Feel free to leave questions in the comments, I&#8217;ll be happy to answer.</p>
<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price&#8217;s blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-automatically-create-getter-and-setter-methods-in-php/">How to Automatically Create Getter and Setter Methods in PHP</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanprice.ca/how-to-automatically-create-getter-and-setter-methods-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Resize an Image with PHP</title>
		<link>http://www.ryanprice.ca/how-to-resize-an-image-with-php/</link>
		<comments>http://www.ryanprice.ca/how-to-resize-an-image-with-php/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 18:35:36 +0000</pubDate>
		<dc:creator>Ryan Price</dc:creator>
				<category><![CDATA[PHP Snippets]]></category>

		<guid isPermaLink="false">http://www.ryanprice.ca/work/?p=27</guid>
		<description><![CDATA[The code shown here will take an image on your server (.jpg only) and resize it to your specifications. $forcedwidth: the width you want the resized image to be. $forcedheight: the height you want the resized image to be. $sourcefile: the path to the file on your server. $destfile: where you want the finished file [...]<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-resize-an-image-with-php/">How to Resize an Image with PHP</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The code shown here will take an image on your server (.jpg only) and resize it to your specifications.</p>
<p><tt>$forcedwidth</tt>: the width you want the resized image to be.<br />
<tt>$forcedheight</tt>: the height you want the resized image to be.<br />
<tt>$sourcefile</tt>: the path to the file on your server.<br />
<tt>$destfile</tt>: where you want the finished file to be placed.<br />
<span id="more-27"></span></p>
<pre name="code" class="php">
//will resize an image to the specified width and height
function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile) {
	$fw = $forcedwidth;
	$fh = $forcedheight;
	$is = getimagesize($sourcefile);

	//set the orientation of the image
	if ($is[0] >= $is[1]) {
		$orientation = 0;
	} else {
		$orientation = 1;
		$fw = $forcedheight;
		$fh = $forcedwidth;
	}

	//resize calculations
	if ($is[0] > $fw || $is[1] > $fh) {
		if (($is[0] - $fw) >= ($is[1] - $fh)) {
			$iw = $fw;
			$ih = ($fw / $is[0]) * $is[1];
		} else {
			$ih = $fh;
			$iw = ($ih / $is[1]) * $is[0];
		}
		$t = 1;
	} else {
		$iw = $is[0];
		$ih = $is[1];
		$t = 2;
	}

	//create the new image
	if ($t == 1) {
		$img_src = imagecreatefromjpeg($sourcefile);
		$img_dst = imagecreatetruecolor($iw, $ih);
		imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1]);
		if (!imagejpeg($img_dst, $destfile, 90)) {
			exit ();
		}
	} else
		if ($t == 2) {
			copy($sourcefile, $destfile);
		}
}
</pre>
<p>Lines #8-14 here set the orientation of the image.  This way it doesn&#8217;t matter if your image is tall for fat, its taken care of.</p>
<p>As mentioned above, this particular function only deals with .jpg type files.  You can easily adjust this to deal with other types as well.  Simply replace the <tt>imagecreatefromjpeg()</tt> function on line 34 with the function that suits you file type.  The most common options are <tt>imagecreatefrompng</tt> and <tt>imagecreatefromgif</tt>, although there are a few others as well. </p>
<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price&#8217;s blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-resize-an-image-with-php/">How to Resize an Image with PHP</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanprice.ca/how-to-resize-an-image-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Remove Items from an Array and Adjust (Reset) Array Keys</title>
		<link>http://www.ryanprice.ca/how-to-remove-items-from-an-array-and-adjust-reset-array-keys/</link>
		<comments>http://www.ryanprice.ca/how-to-remove-items-from-an-array-and-adjust-reset-array-keys/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 15:39:18 +0000</pubDate>
		<dc:creator>Ryan Price</dc:creator>
				<category><![CDATA[PHP Snippets]]></category>

		<guid isPermaLink="false">http://www.ryanprice.ca/work/?p=26</guid>
		<description><![CDATA[There will be a time when you want to delete an item from the array and then want to keep the array indexed numerically without any gaps. This can be done with the unset() and array_values() functions as shown below. $myArray = array("a", "b", "c", "d"); //create the array unset($myArray[2]); //this will erase "c" print_r($myArray); [...]<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-remove-items-from-an-array-and-adjust-reset-array-keys/">How to Remove Items from an Array and Adjust (Reset) Array Keys</a></p>
]]></description>
			<content:encoded><![CDATA[<p>There will be a time when you want to delete an item from the array and then want to keep the array indexed numerically without any gaps.  This can be done with the <tt>unset()</tt> and <tt>array_values()</tt> functions as shown below.<br />
<span id="more-26"></span></p>
<pre name="code" class="php">
$myArray = array("a", "b", "c", "d"); //create the array

unset($myArray[2]); //this will erase "c"

print_r($myArray);

/* Result:
Array
(
    [0] => a
    [1] => b
    [3] => d
)
*/

$myArray = array_values($myArray); //reset the indexes

print_r($my_array);

/* Result:
Array
(
    [0] => a
    [1] => b
    [2] => d
)
*/
</pre>
<p>It is important to note that if you are using associative arrays that the <tt>array_values</tt> function will reindex your array numerically and your associative keys will be lost.</p>
<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price&#8217;s blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-remove-items-from-an-array-and-adjust-reset-array-keys/">How to Remove Items from an Array and Adjust (Reset) Array Keys</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanprice.ca/how-to-remove-items-from-an-array-and-adjust-reset-array-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Show the Contents of an Array, Object, or Array of Objects using PHP</title>
		<link>http://www.ryanprice.ca/how-to-show-the-contents-of-an-array-object-or-array-of-objects-using-php/</link>
		<comments>http://www.ryanprice.ca/how-to-show-the-contents-of-an-array-object-or-array-of-objects-using-php/#comments</comments>
		<pubDate>Fri, 08 Jun 2007 17:15:28 +0000</pubDate>
		<dc:creator>Ryan Price</dc:creator>
				<category><![CDATA[PHP Snippets]]></category>

		<guid isPermaLink="false">http://www.ryanprice.ca/work/how-to-show-the-contents-of-an-array-object-or-array-of-objects-using-php/</guid>
		<description><![CDATA[There's often times when you're writing a script that you want to stop and check to make sure that your data is what you think it is before you continue. Using the print_r() function is the answer. Combine it with pre tags for easy reading: PLAIN TEXT PHP: echo "&#60;pre&#62;"; print_r&#40;$yourVariable&#41;; echo "&#60;/pre&#62;"; &#160; //if [...]<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-show-the-contents-of-an-array-object-or-array-of-objects-using-php/">How to Show the Contents of an Array, Object, or Array of Objects using PHP</a></p>
]]></description>
			<content:encoded><![CDATA[<p>There's often times when you're writing a script that you want to stop and check to make sure that your data is what you think it is before you continue.  Using the <tt>print_r()</tt> function is the answer.  Combine it with <tt>pre</tt> tags for easy reading:</p>
<div class="igBar"><span id="lphp-3"><a href="#" onclick="javascript:showPlainTxt('php-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-3">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"&lt;pre&gt;"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$yourVariable</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"&lt;/pre&gt;"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//if you want to stop the script after viewing the contents you can add this line too:</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Info was printed"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
<span id="more-22"></span></p>
<p>If you're doing a lot of debugging of this type as you go, it will be quicker to make this a function within your file like so:</p>
<div class="igBar"><span id="lphp-4"><a href="#" onclick="javascript:showPlainTxt('php-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-4">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> checkContents<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$myVar</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"&lt;pre&gt;"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$myVar</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"&lt;/pre&gt;"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Then you can quickly check the contents at any point in your script using <tt>checkContents($someVar);</tt>.</p>
<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-show-the-contents-of-an-array-object-or-array-of-objects-using-php/">How to Show the Contents of an Array, Object, or Array of Objects using PHP</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanprice.ca/how-to-show-the-contents-of-an-array-object-or-array-of-objects-using-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Shorthand for the If-Else Statement: The Ternary Operator</title>
		<link>http://www.ryanprice.ca/php-shorthand-for-the-if-else-statement-the-ternary-operator/</link>
		<comments>http://www.ryanprice.ca/php-shorthand-for-the-if-else-statement-the-ternary-operator/#comments</comments>
		<pubDate>Wed, 23 May 2007 18:47:22 +0000</pubDate>
		<dc:creator>Ryan Price</dc:creator>
				<category><![CDATA[PHP Snippets]]></category>

		<guid isPermaLink="false">http://www.ryanprice.ca/work/?p=19</guid>
		<description><![CDATA[The Ternary Operator is shorthand for an if-else statement in PHP. It lets you shrink the size of the statement which in many cases will make your code more readable. The ternary operator has the following structure: (expression 1) ? (expression 2) : (expression 3) If expression 1 is true, then it evaluates expression 2. [...]<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/php-shorthand-for-the-if-else-statement-the-ternary-operator/">PHP Shorthand for the If-Else Statement: The Ternary Operator</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The Ternary Operator is shorthand for an <tt>if-else</tt> statement in PHP.  It lets you shrink the size of the statement which in many cases will make your code more readable.  The ternary operator has the following structure:</p>
<p><tt>(expression 1) ? (expression 2) : (expression 3)</tt></p>
<p>If expression 1 is true, then it evaluates expression 2.<br />
If expression 1 is false, then it evaluates expression 3. </p>
<p>Here's an example:</p>
<div class="igBar"><span id="lphp-6"><a href="#" onclick="javascript:showPlainTxt('php-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-6">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//this statement is the long version</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$pitch</span> == <span style="color:#FF0000;">"strike three"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$batter</span> = <span style="color:#FF0000;">"out"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$batter</span> = <span style="color:#FF0000;">"not out"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//but we can re-write it using the Ternary Operator:</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$batter</span> = <span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$pitch</span> == <span style="color:#FF0000;">"strike three"</span>&nbsp; ? <span style="color:#FF0000;">"out"</span> : <span style="color:#FF0000;">"not out"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/php-shorthand-for-the-if-else-statement-the-ternary-operator/">PHP Shorthand for the If-Else Statement: The Ternary Operator</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanprice.ca/php-shorthand-for-the-if-else-statement-the-ternary-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Ensure You&#8217;re Using The Latest Version of PHP Functions</title>
		<link>http://www.ryanprice.ca/how-to-ensure-youre-using-the-latest-version-of-php-functions/</link>
		<comments>http://www.ryanprice.ca/how-to-ensure-youre-using-the-latest-version-of-php-functions/#comments</comments>
		<pubDate>Wed, 16 May 2007 19:36:55 +0000</pubDate>
		<dc:creator>Ryan Price</dc:creator>
				<category><![CDATA[PHP Snippets]]></category>

		<guid isPermaLink="false">http://www.ryanprice.ca/work/?p=17</guid>
		<description><![CDATA[As with all things when they are upgraded, PHP 5 deprecated some functions from previous versions - specifically PHP 4.x. As you upgrade to new versions of PHP you'll want to make sure you're taking advantage of the upgrades. How can you tell when you're using deprecated functions? The answer is in the error_reporting() level [...]<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-ensure-youre-using-the-latest-version-of-php-functions/">How to Ensure You&#8217;re Using The Latest Version of PHP Functions</a></p>
]]></description>
			<content:encoded><![CDATA[<p>As with all things when they are upgraded, PHP 5 <acronym title="the gradual phasing-out of a software or programming language feature.">deprecated</acronym> some functions from previous versions - specifically PHP 4.x.</p>
<p>As you upgrade to new versions of PHP you'll want to make sure you're taking advantage of the upgrades.  How can you tell when you're using deprecated functions?  The answer is in the <tt>error_reporting()</tt> level you set.<br />
<span id="more-17"></span></p>
<p>By setting it to <tt>E_STRICT</tt> (integer value 2048) you will receive warnings when you are using deprecated functions.</p>
<div class="igBar"><span id="lphp-8"><a href="#" onclick="javascript:showPlainTxt('php-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-8">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/error_reporting"><span style="color:#000066;">error_reporting</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">2048</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>From the PHP site:</p>
<blockquote><p>
In PHP 5 a new error level E_STRICT is available. As E_STRICT is not included within E_ALL you have to explicitly enable this kind of error level. Enabling E_STRICT during development has some benefits. STRICT messages will help you to use the latest and greatest suggested method of coding, for example warn you about using deprecated functions.</p></blockquote>
<p>This may not be something you'll want to leave set at this level all the time.  However, setting it every once in a while and checking your code will ensure that you're up to date on the functions you should be using.</p>
<p>You can also <a href="http://livedocs.coggeshall.org/en/migration5.html" target="_blank">view a complete list of new functions</a> along with all what ones now behave differently.</p>
<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-ensure-youre-using-the-latest-version-of-php-functions/">How to Ensure You&#8217;re Using The Latest Version of PHP Functions</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanprice.ca/how-to-ensure-youre-using-the-latest-version-of-php-functions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Make Your Form Data Safe for SQL with PHP</title>
		<link>http://www.ryanprice.ca/how-to-make-your-form-data-safe-for-mysql-with-php/</link>
		<comments>http://www.ryanprice.ca/how-to-make-your-form-data-safe-for-mysql-with-php/#comments</comments>
		<pubDate>Thu, 10 May 2007 13:52:04 +0000</pubDate>
		<dc:creator>Ryan Price</dc:creator>
				<category><![CDATA[PHP Snippets]]></category>

		<guid isPermaLink="false">http://www.ryanprice.ca/work/?p=10</guid>
		<description><![CDATA[Whenever you are entering user-generated data from a form in any kind of SQL query you have to make sure you're safe. Using the mysql_real_escape_string() function will ensure this. Not using this on each and every piece of form data will leave you susceptible to what are called SQL Injection Attacks. PLAIN TEXT PHP: $safeVariable [...]<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-make-your-form-data-safe-for-mysql-with-php/">How to Make Your Form Data Safe for SQL with PHP</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Whenever you are entering user-generated data from a form in any kind of SQL query you have to make sure you're safe.  Using the <tt>mysql_real_escape_string()</tt> function will ensure this.  Not using this on each and every piece of form data will leave you susceptible to what are called SQL Injection Attacks.</p>
<div class="igBar"><span id="lphp-10"><a href="#" onclick="javascript:showPlainTxt('php-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-10">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$safeVariable</span> = <a href="http://www.php.net/mysql_real_escape_string"><span style="color:#000066;">mysql_real_escape_string</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'unsafeVariable'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>To learn more about why this is important, check out these articles:<br />
<a href="http://www.unixwiz.net/techtips/sql-injection.html" target="_blank">SQL Injection Attacks by Example</a><br />
<a href="http://www.codeproject.com/cs/database/SqlInjectionAttacks.asp" target="_blank">More examples and explanations</a></p>
<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-make-your-form-data-safe-for-mysql-with-php/">How to Make Your Form Data Safe for SQL with PHP</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanprice.ca/how-to-make-your-form-data-safe-for-mysql-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Debug MySQL Errors</title>
		<link>http://www.ryanprice.ca/how-to-debug-mysql-errors/</link>
		<comments>http://www.ryanprice.ca/how-to-debug-mysql-errors/#comments</comments>
		<pubDate>Fri, 27 Apr 2007 20:26:23 +0000</pubDate>
		<dc:creator>Ryan Price</dc:creator>
				<category><![CDATA[PHP Snippets]]></category>

		<guid isPermaLink="false">http://www.ryanprice.ca/work/?p=4</guid>
		<description><![CDATA[Using mySQL to connect, retrieve and place data into a database is very useful. A lot can go wrong in this process. Fortunately, any errors are usually fairly easy to track down using the wonderful mysql_error() function to let you know exactly what went wrong. A couple examples: PLAIN TEXT PHP: mysql_select_db&#40;MYSQL_DB&#41; or die &#40;"Invalid [...]<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-debug-mysql-errors/">How to Debug MySQL Errors</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Using mySQL to connect, retrieve and place data into a database is very useful.  A lot can go wrong in this process.  Fortunately, any errors are usually fairly easy to track down using the wonderful <tt>mysql_error()</tt> function to let you know exactly what went wrong.</p>
<p>A couple examples:</p>
<div class="igBar"><span id="lphp-13"><a href="#" onclick="javascript:showPlainTxt('php-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-13">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/mysql_select_db"><span style="color:#000066;">mysql_select_db</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>MYSQL_DB<span style="color:#006600; font-weight:bold;">&#41;</span> or <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Invalid select: "</span> . <a href="http://www.php.net/mysql_error"><span style="color:#000066;">mysql_error</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div class="igBar"><span id="lphp-14"><a href="#" onclick="javascript:showPlainTxt('php-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-14">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM table WHERE something='somethingElse' "</span><span style="color:#006600; font-weight:bold;">&#41;</span> or <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Invalid query: "</span> . <a href="http://www.php.net/mysql_error"><span style="color:#000066;">mysql_error</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This post is from <a href="http://www.ryanprice.ca">Ryan Price's blog</a>.<br/><br/><a href="http://www.ryanprice.ca/how-to-debug-mysql-errors/">How to Debug MySQL Errors</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanprice.ca/how-to-debug-mysql-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
