<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Good and Bad PHP Code</title>
	<atom:link href="http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/</link>
	<description></description>
	<pubDate>Sat, 05 Jul 2008 20:52:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Befara</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-379972</link>
		<dc:creator>Befara</dc:creator>
		<pubDate>Thu, 13 Sep 2007 20:08:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-379972</guid>
		<description>My feeling is that your above example is something that can be taught fairly easily. Granted, it's desirable to have a candidate who has enought experience to know what the major security vulnerabilities are and how to address them, but I'm more interested in whether a developer "gets it" -- that is understands the overriding concepts in architecting good code.

When I interview a LAMP candidate I'm usually looking first at what kind of OOP experience they have. Do they know the difference between an object and a class? Do they understand inheritance, extending classes? Have they ever used any common design patterns? Singletons? Factory methods? Do they know what $this is? If you have an kind of enterprise codebase, it's essential to understand the big picture if you are going to make meaningful contributions.

I would be extremely happy in your above example if a developer simply said to me: "User input should never be trusted, the $_GET argument should be run through a sanitation method then placed in an appropriately named array, i.e. $clean['query']".</description>
		<content:encoded><![CDATA[<p>My feeling is that your above example is something that can be taught fairly easily. Granted, it&#8217;s desirable to have a candidate who has enought experience to know what the major security vulnerabilities are and how to address them, but I&#8217;m more interested in whether a developer &#8220;gets it&#8221; &#8212; that is understands the overriding concepts in architecting good code.</p>
<p>When I interview a LAMP candidate I&#8217;m usually looking first at what kind of OOP experience they have. Do they know the difference between an object and a class? Do they understand inheritance, extending classes? Have they ever used any common design patterns? Singletons? Factory methods? Do they know what $this is? If you have an kind of enterprise codebase, it&#8217;s essential to understand the big picture if you are going to make meaningful contributions.</p>
<p>I would be extremely happy in your above example if a developer simply said to me: &#8220;User input should never be trusted, the $_GET argument should be run through a sanitation method then placed in an appropriately named array, i.e. $clean[&#8217;query&#8217;]&#8221;.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: marnen</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-322354</link>
		<dc:creator>marnen</dc:creator>
		<pubDate>Wed, 01 Aug 2007 18:54:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-322354</guid>
		<description>&lt;blockquote&gt;Passing the ENT_QUOTES argument to htmlspecialchars to ensure that single quotes (') are also escaped isn’t strictly necessary in this case, but it’s a good habit to get into.&lt;/blockquote&gt;&lt;p&gt;Um, why do this if it "isn't strictly necessary"? As far as I can tell, this practice is of no use whatsoever in this or any other case. If you're escaping &#60;, &#62;, &#38;, and perhaps &#34;, that's all you need.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<blockquote><p>Passing the ENT_QUOTES argument to htmlspecialchars to ensure that single quotes (&#8217;) are also escaped isn’t strictly necessary in this case, but it’s a good habit to get into.</p></blockquote>
<p>Um, why do this if it &#8220;isn&#8217;t strictly necessary&#8221;? As far as I can tell, this practice is of no use whatsoever in this or any other case. If you&#8217;re escaping &lt;, &gt;, &amp;, and perhaps &quot;, that&#8217;s all you need.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Hans Kejser Hansen</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-297683</link>
		<dc:creator>Hans Kejser Hansen</dc:creator>
		<pubDate>Sun, 08 Jul 2007 12:01:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-297683</guid>
		<description>Don't think You better example is an example on good php code. I alway s uses wrapper function when calling htmlpecialchars, so i easyly can change the third parameter if needed. (Guess I will find a job another place, where they avoid redundant code.)

"Passing the ENT_QUOTES argument to htmlspecialchars to ensure that single quotes (') are also escaped isn’t strictly necessary in this case, but it’s a good habit to get into."
Very dependent on the context, fx can be a very bad habbit when used i generateed javascript. Also a place where I prefer a wrapper function.

And the little issues about () around echo, I would read the company's code standard, before I removed it.

And one thing IHMO that makes good PHP code, is the planning before You opens the editor.</description>
		<content:encoded><![CDATA[<p>Don&#8217;t think You better example is an example on good php code. I alway s uses wrapper function when calling htmlpecialchars, so i easyly can change the third parameter if needed. (Guess I will find a job another place, where they avoid redundant code.)</p>
<p>&#8220;Passing the ENT_QUOTES argument to htmlspecialchars to ensure that single quotes (&#8217;) are also escaped isn’t strictly necessary in this case, but it’s a good habit to get into.&#8221;<br />
Very dependent on the context, fx can be a very bad habbit when used i generateed javascript. Also a place where I prefer a wrapper function.</p>
<p>And the little issues about () around echo, I would read the company&#8217;s code standard, before I removed it.</p>
<p>And one thing IHMO that makes good PHP code, is the planning before You opens the editor.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Webx</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-271819</link>
		<dc:creator>Webx</dc:creator>
		<pubDate>Mon, 11 Jun 2007 05:47:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-271819</guid>
		<description>PHP simply is not being taken seriously for many reasons. A perfect example to this thought process focuses on this general discussion actually, in which nobody can agree on anything. The same is present and constantly persistent with PHP code and how to do something the "right" way.

Sessions, security and even Cookies, all have the same areas that compound with a vast amount of opinions, too many ways of producing the same code (OOP or simplified functions) that do the same thing and way too many controversies on how to produce the most secure code with no real answer.

You can simply confuse yourself to the point of mental breakdown just looking for a correct way of handling security issues pertaining to sessions.

And no, that is not a good thing. Having too many options to produce code is why PHP is constantly under the microscope and controversy of "secure applications" and why most employers dont take PHP seriously. After all, the general misconception is that most of us that develop PHP applications are simply people that stumbled onto a easy format to program with and as a result, think we are developers. The same thought process exudes highly in most college campuses that rely stringently on Flash, Java and .NET curriculum. When you walk into a college and see a Microsoft plaque of recognition and sponsorship hanging on the wall, you can pretty much forget programming in PHP.

So, what does that have to do with anything? Where do you go to school to get a Bachelors degree in PHP programming? Where do you go to get a Masters degree in PHP programming? Nowhere really. At least around here. There is no such thing. But, if you want to be a .NET, Java or Flash developer, I can show you 30 jobs that I received in just the last 4 days.

Employers want to see that Degree hanging on your wall and use that degree to base their pay rates upon, whether or not the technology you use is actually better than what they are using.

Just think of it like this, to make it real short and sweet. Why do most employers use M$ servers and not Linux servers?</description>
		<content:encoded><![CDATA[<p>PHP simply is not being taken seriously for many reasons. A perfect example to this thought process focuses on this general discussion actually, in which nobody can agree on anything. The same is present and constantly persistent with PHP code and how to do something the &#8220;right&#8221; way.</p>
<p>Sessions, security and even Cookies, all have the same areas that compound with a vast amount of opinions, too many ways of producing the same code (OOP or simplified functions) that do the same thing and way too many controversies on how to produce the most secure code with no real answer.</p>
<p>You can simply confuse yourself to the point of mental breakdown just looking for a correct way of handling security issues pertaining to sessions.</p>
<p>And no, that is not a good thing. Having too many options to produce code is why PHP is constantly under the microscope and controversy of &#8220;secure applications&#8221; and why most employers dont take PHP seriously. After all, the general misconception is that most of us that develop PHP applications are simply people that stumbled onto a easy format to program with and as a result, think we are developers. The same thought process exudes highly in most college campuses that rely stringently on Flash, Java and .NET curriculum. When you walk into a college and see a Microsoft plaque of recognition and sponsorship hanging on the wall, you can pretty much forget programming in PHP.</p>
<p>So, what does that have to do with anything? Where do you go to school to get a Bachelors degree in PHP programming? Where do you go to get a Masters degree in PHP programming? Nowhere really. At least around here. There is no such thing. But, if you want to be a .NET, Java or Flash developer, I can show you 30 jobs that I received in just the last 4 days.</p>
<p>Employers want to see that Degree hanging on your wall and use that degree to base their pay rates upon, whether or not the technology you use is actually better than what they are using.</p>
<p>Just think of it like this, to make it real short and sweet. Why do most employers use M$ servers and not Linux servers?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Soaplady</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-271717</link>
		<dc:creator>Soaplady</dc:creator>
		<pubDate>Mon, 11 Jun 2007 03:07:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-271717</guid>
		<description>I started using PHP when I had databse experience but no other programming skill.  Now, several websites and many hours of reading and practice, I can say that I did get most of the points for "good programming" right.

It is true that anyone with a little smarts can create a decent (and sometimes pretty sophisticated) php website.  And that, I think, is what most employers see.  

However, the real difficulty is that getting from the decent to the elegant and from the pretty safe to the really secure is a hard road.  There are few available classes and very little training exists.  Learning from books and the school of hard knocks is difficult, especially when there are many ways of doing just about everything in PHP.

I've gotten to the point where I do get paid for my work as a contractor building database-driven websites. Maybe not a LOT, but enough so that I feel decent about the resultant hourly wage.

Most of the "how-to" materials for advanced php programming assumes an understanding of some other language. Available information is often contradictory and incomplete.  The php manual is excellent, but only if you know what you're looking for to start with.

PHP programmers will probably become more standardly paid when there is a clearly defined path charting the beginner, novice, amateur, advanced, professional and master.  It will also be easier for php programmers (and scripters) to get better at their skills.  

Once there are more truly professional and master php programmers, then we'll see more advanced enterprise websites built and running with php.  And THAT is what will get higher pay for all of us.</description>
		<content:encoded><![CDATA[<p>I started using PHP when I had databse experience but no other programming skill.  Now, several websites and many hours of reading and practice, I can say that I did get most of the points for &#8220;good programming&#8221; right.</p>
<p>It is true that anyone with a little smarts can create a decent (and sometimes pretty sophisticated) php website.  And that, I think, is what most employers see.  </p>
<p>However, the real difficulty is that getting from the decent to the elegant and from the pretty safe to the really secure is a hard road.  There are few available classes and very little training exists.  Learning from books and the school of hard knocks is difficult, especially when there are many ways of doing just about everything in PHP.</p>
<p>I&#8217;ve gotten to the point where I do get paid for my work as a contractor building database-driven websites. Maybe not a LOT, but enough so that I feel decent about the resultant hourly wage.</p>
<p>Most of the &#8220;how-to&#8221; materials for advanced php programming assumes an understanding of some other language. Available information is often contradictory and incomplete.  The php manual is excellent, but only if you know what you&#8217;re looking for to start with.</p>
<p>PHP programmers will probably become more standardly paid when there is a clearly defined path charting the beginner, novice, amateur, advanced, professional and master.  It will also be easier for php programmers (and scripters) to get better at their skills.  </p>
<p>Once there are more truly professional and master php programmers, then we&#8217;ll see more advanced enterprise websites built and running with php.  And THAT is what will get higher pay for all of us.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: ivanfx</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-271152</link>
		<dc:creator>ivanfx</dc:creator>
		<pubDate>Sun, 10 Jun 2007 14:13:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-271152</guid>
		<description>Good old Notepad...</description>
		<content:encoded><![CDATA[<p>Good old Notepad&#8230;</p>]]></content:encoded>
	</item>
	<item>
		<title>By: praveen</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-270473</link>
		<dc:creator>praveen</dc:creator>
		<pubDate>Sat, 09 Jun 2007 19:16:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-270473</guid>
		<description>Hi,

I have worked on PHP/ JSP/ .NET but have a feeling that.. compared to ASP.net and Java, PHP don't have a proper IDE. I am aware that lot of IDE's from zend, eclipse, Nusphere etc are there in the market. But personally I don't find any of them useful and handy like Visual studio or Net beans.

I will appreciate your comments on this. And will like to know how you are managing it?</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I have worked on PHP/ JSP/ .NET but have a feeling that.. compared to ASP.net and Java, PHP don&#8217;t have a proper IDE. I am aware that lot of IDE&#8217;s from zend, eclipse, Nusphere etc are there in the market. But personally I don&#8217;t find any of them useful and handy like Visual studio or Net beans.</p>
<p>I will appreciate your comments on this. And will like to know how you are managing it?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: MickoZ</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-269787</link>
		<dc:creator>MickoZ</dc:creator>
		<pubDate>Fri, 08 Jun 2007 19:32:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-269787</guid>
		<description>Like someone said earlier, your "Better" rewrite alters the original version (but of course, in practice, it would depend of the context the code is executed), so I will go for something like this:

&lt;pre&gt;&lt;code class="php"&gt;
&#60;?php
$query = isset($_GET['query']) ? (string) $_GET['query'] : '';
echo '&#60;p&#62;Search results for query: ' . htmlspecialchars($query) . '.&#60;/p&#62;';
?&#62;
&lt;/code&gt;&lt;/pre&gt;

- I accept/encourage filtering (type casting), when this is the type of the interface.
  - Casting a text input to int would be a bad example of filtering (for multiple reasons).
- $query could equal '' or null depending on the behaviour/semantic you want. ;-)

Personaly I won't care much about single vs. double quote, etc. -- but to each his owns.  I will probably give more values to someone who is good in analysis, do good design, etc. --- it is much easier after to optimize stuff in the case it is needed.  Your example was just an example anyway and you probably go further than all these. ;-)

Having said that, I'm a kind-of generalist.  I believe that someone who is a good developer, will be good in PHP, Java, Ruby, etc. [even with different language like Haskell, Prolog, etc.] -- because at the analysis, design point, even construction/coding, testing, etc. the thinking and output will be very similar (even if the language can drive a part of our solution).  So I will mostly seek for a good developer.

Of course sometime, you want/need someone "ready" to do the job with the solution you have used (e.g. PHP).  However if you plan to hire someone for the next 2-5 years, chance are a good developers will do a better job even with the learning overhead.  I guess it is a dilemma with skill/potential vs. experience.  Hopefully you find both at the same time. ;-)

Actually, I would risk to say that there is a big chance that a good developer will be good in other fields of work, but the overhead might be too high (e.g. become a good lawyer/doctor/etc. in a month without experience/knowledge).  But like I said, this is risky, because some people are very "smart", but are not athletics, nor good musician, etc. or simply have no interest at being good at something beside their potential, skill and knowledge.

Amen.</description>
		<content:encoded><![CDATA[<p>Like someone said earlier, your &#8220;Better&#8221; rewrite alters the original version (but of course, in practice, it would depend of the context the code is executed), so I will go for something like this:</p>
<pre><code class="php">
&lt;?php
$query = isset($_GET['query']) ? (string) $_GET['query'] : '';
echo '&lt;p&gt;Search results for query: ' . htmlspecialchars($query) . '.&lt;/p&gt;';
?&gt;
</code></pre>
<p>- I accept/encourage filtering (type casting), when this is the type of the interface.<br />
  - Casting a text input to int would be a bad example of filtering (for multiple reasons).<br />
- $query could equal &#8221; or null depending on the behaviour/semantic you want. ;-)</p>
<p>Personaly I won&#8217;t care much about single vs. double quote, etc. &#8212; but to each his owns.  I will probably give more values to someone who is good in analysis, do good design, etc. &#8212; it is much easier after to optimize stuff in the case it is needed.  Your example was just an example anyway and you probably go further than all these. ;-)</p>
<p>Having said that, I&#8217;m a kind-of generalist.  I believe that someone who is a good developer, will be good in PHP, Java, Ruby, etc. [even with different language like Haskell, Prolog, etc.] &#8212; because at the analysis, design point, even construction/coding, testing, etc. the thinking and output will be very similar (even if the language can drive a part of our solution).  So I will mostly seek for a good developer.</p>
<p>Of course sometime, you want/need someone &#8220;ready&#8221; to do the job with the solution you have used (e.g. PHP).  However if you plan to hire someone for the next 2-5 years, chance are a good developers will do a better job even with the learning overhead.  I guess it is a dilemma with skill/potential vs. experience.  Hopefully you find both at the same time. ;-)</p>
<p>Actually, I would risk to say that there is a big chance that a good developer will be good in other fields of work, but the overhead might be too high (e.g. become a good lawyer/doctor/etc. in a month without experience/knowledge).  But like I said, this is risky, because some people are very &#8220;smart&#8221;, but are not athletics, nor good musician, etc. or simply have no interest at being good at something beside their potential, skill and knowledge.</p>
<p>Amen.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: paulmasri</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-269704</link>
		<dc:creator>paulmasri</dc:creator>
		<pubDate>Fri, 08 Jun 2007 18:17:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-269704</guid>
		<description>I have to disagree with philmee. We regularly build large-scale apps with PHP &#38; MySQL. We've developed our own library of code (including our own PHP templates) to maximise code reuse and enable rapid development. As a result we rarely have to revisit 'low-level code' unless we're adding significant new functionality across the board.

Take &lt;a href="http://www.bryntegbooks.co.uk" rel="nofollow"&gt;Brynteg Books&lt;/a&gt; for example. A database of over 2 million books and a custom search engine. Yet each page requires only 3-4 SQL queries.

Likewise take a look at &lt;a href="http://www.miamasri.com" rel="nofollow"&gt;Mia Masri (wedding tiaras &#38; jewellery)&lt;/a&gt; which has a PHP/MySQL content management system behind the scenes. Much of the PHP work happens behind the scenes, again with 3-4 SQL queries. And there's dynamic use of PHP + Flash within the &lt;a href="http://www.miamasri.com/_tiaradesigner/" rel="nofollow"&gt;Design-a-Tiara&lt;/a&gt; feature that is a state-of-the-art web app.

I come back to my earlier point (and to bring this back on topic), the quality of a PHP coder is more about the quality of the programmer as a programmer rather than the specifics of this language. And that's where the value lies in hiring a PHP coder.</description>
		<content:encoded><![CDATA[<p>I have to disagree with philmee. We regularly build large-scale apps with PHP &amp; MySQL. We&#8217;ve developed our own library of code (including our own PHP templates) to maximise code reuse and enable rapid development. As a result we rarely have to revisit &#8216;low-level code&#8217; unless we&#8217;re adding significant new functionality across the board.</p>
<p>Take <a href="http://www.bryntegbooks.co.uk" rel="nofollow">Brynteg Books</a> for example. A database of over 2 million books and a custom search engine. Yet each page requires only 3-4 SQL queries.</p>
<p>Likewise take a look at <a href="http://www.miamasri.com" rel="nofollow">Mia Masri (wedding tiaras &amp; jewellery)</a> which has a PHP/MySQL content management system behind the scenes. Much of the PHP work happens behind the scenes, again with 3-4 SQL queries. And there&#8217;s dynamic use of PHP + Flash within the <a href="http://www.miamasri.com/_tiaradesigner/" rel="nofollow">Design-a-Tiara</a> feature that is a state-of-the-art web app.</p>
<p>I come back to my earlier point (and to bring this back on topic), the quality of a PHP coder is more about the quality of the programmer as a programmer rather than the specifics of this language. And that&#8217;s where the value lies in hiring a PHP coder.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: philmee95</title>
		<link>http://www.sitepoint.com/blogs/2007/05/25/good-and-bad-php-code/#comment-269684</link>
		<dc:creator>philmee95</dc:creator>
		<pubDate>Fri, 08 Jun 2007 17:34:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=1929#comment-269684</guid>
		<description>Wow, talk about incoherent thought pattern (my above post). Anyways I was just trying to say it is too time consuming to build a full enterprise app in php right now; A few of my projects look like java projects with a million 3rd party includes.  
Anyone ever looked into all of those frameworks, especially the CMS projects with panel arrangement code (web parts-ish). They all make 400 SQL calls per page and had a few of the higher level organizations I interviewed with mention that as a big drawback to PHP. So again, with better RAD tools (Zend is almost there besides missing WYSIWYG) and some decent frameworks PHP may make it there. 
The boon to PHP is the open source so we can all learn and share. This might also be it's biggest disadvantage in that the bad habbits coders pick up and the fact that there are so many projects out there that companies don't want to pay for it. They just download a forum and now they are running LAMP like all the others. The minute you try to change those forums a bit, you dive into a mess of transaction scripts, scratch your head a bit and say it is not worth it.</description>
		<content:encoded><![CDATA[<p>Wow, talk about incoherent thought pattern (my above post). Anyways I was just trying to say it is too time consuming to build a full enterprise app in php right now; A few of my projects look like java projects with a million 3rd party includes.<br />
Anyone ever looked into all of those frameworks, especially the CMS projects with panel arrangement code (web parts-ish). They all make 400 SQL calls per page and had a few of the higher level organizations I interviewed with mention that as a big drawback to PHP. So again, with better RAD tools (Zend is almost there besides missing WYSIWYG) and some decent frameworks PHP may make it there.<br />
The boon to PHP is the open source so we can all learn and share. This might also be it&#8217;s biggest disadvantage in that the bad habbits coders pick up and the fact that there are so many projects out there that companies don&#8217;t want to pay for it. They just download a forum and now they are running LAMP like all the others. The minute you try to change those forums a bit, you dive into a mess of transaction scripts, scratch your head a bit and say it is not worth it.</p>]]></content:encoded>
	</item>
</channel>
</rss>
