<?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>CodingExperiments.com &#187; PHP</title>
	<atom:link href="http://codingexperiments.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://codingexperiments.com</link>
	<description>$ sudo make money</description>
	<lastBuildDate>Wed, 07 Apr 2010 02:53:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Beginner PHP Programmer Mistakes Part Three: Using PHP 4 (or lower)</title>
		<link>http://codingexperiments.com/beginner-php-programmer-mistakes-part-three-using-php-4-or-lower/</link>
		<comments>http://codingexperiments.com/beginner-php-programmer-mistakes-part-three-using-php-4-or-lower/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 16:28:50 +0000</pubDate>
		<dc:creator>Rishabh Mishra</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://codingexperiments.com/?p=143</guid>
		<description><![CDATA[Introduction PHP 5 has been out for a long, long time. Yet there is a disturbing trend. I still see servers with PHP 4 installed. Why would you have PHP 4 installed? Really, keeping PHP 4 won&#8217;t help you very much in the long run. It might be more convenient as you already know that [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>PHP 5 has been out for a long, long time. Yet there is a disturbing trend. I still see servers with PHP 4 installed. Why would you have PHP 4 installed? Really, keeping PHP 4 won&#8217;t help you very much in the long run. It might be more convenient as you already know that all your script work in PHP 4, but I believe that it is worth it to upgrade to PHP 5.</p>
<h2>Get PHP 5. It&#8217;s good for you.</h2>
<p><strong>1) PHP 5 has features that have been in other languages for a long time.</strong></p>
<p>PHP 5 introduces exceptions. As many beginner programmers use PHP, having them learn PHP 5 exceptions means that they will be familiar with the concept when they move onto other languages.</p>
<p><strong>2) It allows you to use tools that only support PHP 5.</strong></p>
<p>Remember that content management system that you thought would be perfect for your site? But wait! It&#8217;s for PHP 5 only. Since you&#8217;re running a PHP 4 server, you miss out.</p>
<p><strong>3) Because support for PHP 4 has been dropped.</strong></p>
<p>What, so you&#8217;re going to continue using something unsupported when PHP 5 is right there?</p>
<p>Also, if you attempt to ask a developer friend for PHP help, there is a good chance that your developer friend will tell you to upgrade, unless he or she is also running PHP 4. If your friend is running PHP 4, you should send this article to him or her. ^_^</p>
<p><strong>4) You don&#8217;t annoy people that write PHP code for your server.</strong></p>
<p>Let&#8217;s bring back that developer friend of yours. You have a cool idea for a PHP web application. He or she asks which version of PHP 5 you are running. You say, with an embarrassed smile, &#8220;I&#8217;m running PHP 4.4.8.&#8221;</p>
<p>If your developer friends are used to PHP 5, it makes it much more difficult for them to write PHP code for your sever. Friends are good, save them a headache.</p>
]]></content:encoded>
			<wfw:commentRss>http://codingexperiments.com/beginner-php-programmer-mistakes-part-three-using-php-4-or-lower/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beginner PHP Programmer Mistakes Part Two: Going Nutty with Exceptions</title>
		<link>http://codingexperiments.com/beginner-php-programmer-mistakes-part-two-going-nutty-with-exceptions/</link>
		<comments>http://codingexperiments.com/beginner-php-programmer-mistakes-part-two-going-nutty-with-exceptions/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 02:18:12 +0000</pubDate>
		<dc:creator>Rishabh Mishra</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[mistakes]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codingexperiments.com/?p=145</guid>
		<description><![CDATA[You&#8217;re a beginner PHP programmer, and your server has just been upgraded to PHP 5. Yay! Time to rewrite that error handling code to include exceptions! One mistake that beginner PHP programmers make is that they go crazy with exceptions. They might turn the below code: function checkUsername ($username) { if (!is_string ($username)) { die [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;re a beginner PHP programmer, and your server has just been upgraded to PHP 5. Yay! Time to rewrite that error handling code to include exceptions! One mistake that beginner PHP programmers make is that they go crazy with exceptions. They might turn the below code:</p>
<blockquote>
<pre>function checkUsername ($username)
{
    if (!is_string ($username))
    {
        die ('The username is invalid');
        /* Also acceptable are:
        *     trigger_error (&lt;something&gt;);
        *     ^-- If you the proper error handling
        *         setup for site visitors.
        *
        *     return &lt;something&gt;;
        *      ^-- Provided that the caller saves
        *      the return value and that the
        *      value is checked.*/
    }
    else
    {
        return true;
    }
}

oldCheckUsername ($username);</pre>
</blockquote>
<p>into:</p>
<blockquote>
<pre>class OMGYourUsernameIsNotValidException extends
Exception {}

function checkUsername ($username)
{
    if (!is_string ($username))
    {
        throw new OMGYourUsernameIsNotValidException
        ('Username = fail');
    }
    else
    {
        return true;
    }
}

try
{
    checkUsername ($username);
}
catch  (OMGYourUsernameIsNotValidException  $e)
{
    echo $e-&gt;getMessage ();
}</pre>
</blockquote>
<p>Exceptions are best used for things like a database connection failing. They aren&#8217;t really for simple things like form validation.</p>
<p>Uncaught PHP exceptions are considered E_FATAL errors, and I consider that a good measurement of whether or not you need exceptions. While you might want to terminate script execution in the case of improperly filled in forms, you wouldn&#8217;t want to use that error level for form validation.</p>
<p>A failed database connection seems to fit well into E_FATAL, and by that logic, it&#8217;s okay to use exceptions for it.</p>
<p>Disagree? Know a better way to handle errors in PHP? Tell me in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://codingexperiments.com/beginner-php-programmer-mistakes-part-two-going-nutty-with-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beginner PHP Programmer Mistakes Part One: Using Global Variables</title>
		<link>http://codingexperiments.com/beginner-php-programmer-mistakes-part-one-using-global-variables/</link>
		<comments>http://codingexperiments.com/beginner-php-programmer-mistakes-part-one-using-global-variables/#comments</comments>
		<pubDate>Sun, 22 Jun 2008 20:42:55 +0000</pubDate>
		<dc:creator>Rishabh Mishra</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://codingexperiments.com/?p=138</guid>
		<description><![CDATA[I&#8217;ve talked about PHP before. One of the major problems is all the beginners writing code and posting examples that other beginning programmers use. But complaining on and on about beginner code isn&#8217;t actually very useful. What if I actually explained how to write good code in PHP and what sort of code that a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve talked about PHP before. One of the major problems is all the beginners writing code and posting examples that other beginning programmers use. But complaining on and on about beginner code isn&#8217;t actually very useful. What if I actually <em>explained</em> how to write good code in PHP and what sort of code that a PHP programmer should avoid? Well, that would actually be useful. That would accomplish something. That would be another series of posts at CodingExperiments.</p>
<p>So, here goes. This post in the series will cover using global variables in PHP, or rather, why you shouldn&#8217;t use global variables in PHP. First, let&#8217;s see what a global variable is, and what it does.<br />
<code><br />
&lt;?php<br />
$random_variable = 'foo';<br />
$awesome_variable = 'http://codingexperiments.com';<br />
// This works<br />
function bar ()<br />
{<br />
// Making it global makes it work<br />
global $random_variable;<br />
echo $random_variable;<br />
}<br />
bar ();<br />
//<br />
// This doesn't<br />
function website_promotion ()<br />
{<br />
// $awesome_variable is not being made global here<br />
echo '<br />
' . $awesome_variable;<br />
}<br />
website_promotion ();<br />
?&gt;</code></p>
<p>If you were to run that script, you would get &#8220;foo&#8221;, and only &#8220;foo&#8221;. Don&#8217;t believe me? <a href="http://codingexperiments.com/examples/bad_php_code/global_variables.php">Try it out here</a>. Any good programmer will tell you that the URL of this awesome website isn&#8217;t being displayed because of scope. Read more about PHP variable scope here.</p>
<p>So, why are global variables bad? Because they can be changed from anywhere. Global variables are often used for configuration settings, as they will be needed in many places in the program. Configuration settings typically should never change. That&#8217;s why <a title="PHP manual page covering define (); define (); defines constants." href="http://us2.php.net/manual/en/function.define.php">PHP constants</a> are a very good alternative for configuration settings.</p>
<p>If you want a read that really goes in-depth about global variables and so forth, you can check out &#8220;<a href="http://blog.case.edu/gps10/2006/07/22/why_global_variables_in_php_is_bad_programming_practice">Why Global Variables in PHP is Bad Programming Practice</a>&#8220;</p>
]]></content:encoded>
			<wfw:commentRss>http://codingexperiments.com/beginner-php-programmer-mistakes-part-one-using-global-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Proper use of the PHP function debug_backtrace ();</title>
		<link>http://codingexperiments.com/proper-use-of-the-php-function-debug_backtrace/</link>
		<comments>http://codingexperiments.com/proper-use-of-the-php-function-debug_backtrace/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 21:46:13 +0000</pubDate>
		<dc:creator>Rishabh Mishra</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codingexperiments.com/archives/22</guid>
		<description><![CDATA[The PHP function debug_backtrace ( ); is very useful, but it can be dangerous. A beginning developer trying to find a bug on a production-line website might be inclined to use print_r ( ); to show what debug_backtrace ( ); returns, as it returns data in the format of an array. The function print_r ( [...]]]></description>
			<content:encoded><![CDATA[<p>The PHP function <em>debug_backtrace ( );</em> is very useful, but it can be dangerous. A beginning developer trying to find a bug on a production-line website might be inclined to use <em>print_r ( );</em> to show what <em>debug_backtrace ( );</em> returns, as it returns data in the format of an array.</p>
<p>The function <em>print_r ( );</em> displays things directly into the screen. That may not be very wise for a production-line website. After a little thinking, I came up with a secure way to view backtraces. It&#8217;s an easy one-line piece of code that can be inserted into a PHP (5) script.</p>
<blockquote><p>file_put_contents (&#8216;backtrace.txt&#8217;, print_r (debug_backtrace (), true));</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://codingexperiments.com/proper-use-of-the-php-function-debug_backtrace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

