mikebernat.com en-us http://www.mikebernat.com Web-Development, Design, Usability My PHP Best Practices http://www.mikebernat.com/blog/My_PHP_Best_Practices <p><img style="float: right; padding 3px; border: 1px solid #333; background: #fff;" src="http://www.mikebernat.com/images/practice/keyboard.jpg" border="0" alt="Keyboard" width="250" height="158" />PHP's greatest strength is also its greatest weakness. Flexibility. There are an infinite number of ways to perform the same task which PHP will happily do without so much as a peep as to how poor the code really is. Sadly, most developers endure a trial by fire where they only learn from their mistakes after it's too late. </p> <p>I suggest a more retro-active apprach. Studying, surrounding, and forcing yourself to abide by best-practice coding standards will yield surprising results in your applications despite the fact that it may seem like more work than it's worth. </p> <p>I've come up with a list of things that I feel are most important to me when it comes to coding. So, without further adieu:</p> 10/7/2008 12:00pm Things I Hate - People Who Do-Not Know How to Report Bugs http://www.mikebernat.com/blog/Things_I_Hate_-_People_Who_Do-Not_Know_How_to_Report_Bugs <p><img style="float: right; padding 3px; border: 1px solid #333; background: #fff;" src="http://www.mikebernat.com/images/confused-computer-user.jpg" border="0" alt="Bugs" width="150" height="100" />Bugs are an inevitable part of any development project that most people loath or at least generally dislike. If you take the time to examine this phase of a project you will find that it's not the bugs that really irk you, but the way they are presented, described, and handled. </p> <p><span><span>Ok</span>, so maybe bugs are not the worst part about my job but they can be very effective at frustrating the hell out of me. After you get over the fact that you are not the </span><a href="http://mikebernat.com/blog/Egoless_Programming_-_Developing_Without_the_Attitude">world's greatest programmer and are indeed as fallible as the rest of us</a>, you can begin to look at bug reports as an opportunity to better yourself. Learning from your mistakes is a huge part of the job and can be extremely beneficial. However, no matter how vetted any particular programmer may be, most will cringe when they see a bug drop into their inbox. </p> 10/1/2008 1:00pm Installing Bad Behavior Generic - Anti-Spam Solution http://www.mikebernat.com/blog/Installing_Bad_Behavior_Generic_-_Anti-Spam_Solution <p><span>Site spam sucks, no doubt about it. I was getting tired of fighting it manually in hand-to-hand combat and decided to get some help. I'd heard about different techniques for thwarting comment, and other types of form, spam but none of them seemed to make a big enough dent in the problem for my liking. That is, until I tried </span><a href="http://www.bad-behavior.ioerror.us/">Bad Behavior</a>. </p> 7/28/2008 12:00pm Egoless Programming - Developing Without the Attitude http://www.mikebernat.com/blog/Egoless_Programming_-_Developing_Without_the_Attitude <p>One of the most common threads that all programmers share is that of an ego. Some are much worse than others and some have found a way to control or manipulate their ego into a great benefit. Beyond skill-sets and other programming-specific talents I believe the greatest room for personal improvement in programmers as a whole is that of the ego.</p> <p>A quote by <a href="http://www.amazon.com/exec/obidos/search-handle-url?_encoding=UTF8&amp;search-type=ss&amp;index=books&amp;field-author=Gerald%20M.%20Weinberg"><span><span>GeraldWeinberg</span></span></a> from <a href="http://www.amazon.com/exec/obidos/ISBN=0932633420/portlandpatternrA/">The Psychology Of Computer Programming</a>.</p> <blockquote>The idea is that programmers must fight the natural tendency to treat their programs as part of themselves, and therefore to reject all criticism. Rather, they should do their best to treat their designs &amp; implementations as objects independent of themselves, and to view criticism dispassionately on its merits. It's a spiritual discipline that we all fall short of, but that's worth attempting.</blockquote> 7/15/2008 12:00am Installing Xdebug - Best Decision You Will Ever Make http://www.mikebernat.com/blog/Installing_Xdebug_-_Best_Decision_You_Will_Ever_Make <p><img style="float: right; padding 3px; border: 1px solid #333; background: #fff;" src="http://www.mikebernat.com/images/Xdebug/xdebug.png" border="0" alt="xdebug" width="130" height="75" />I finally got around to installing xdebug on my development environment and have decided it is the best thing since sliced bread.Installation was a breeze and the information it provides when something has gone wrong is incredibly helpful during debugging. What I didn't know, and hope to help others by documenting it here, was the amount of configuration options Xdebug has. The base install has some irritating limitations that are easily addressed with a few simple lines in the php.ini file. </p> 7/1/2008 9:00am PHP - Opening and Saving Files over FTP http://www.mikebernat.com/blog/PHP_-_Opening_and_Saving_Files_over_FTP <p><span>PHP makes file system manipulation easy with its variety of built-in functions. One thing I always knew, but never got the chance to try, is that many of those same functions worked over FTP instead of the local file system. I finally got my excuse to give it the ole' college try and I found a few things that may help others with the same task.</span></p> 6/25/2008 6:00pm The Ternary Operator http://www.mikebernat.com/blog/The_Ternary_Operator <p><img style="float: right; padding 3px; border: 1px solid #333; background: #fff;" src="http://www.mikebernat.com/images/php_logo.gif" border="0" alt="PHP" width="70" height="36" />Every good programmer should constantly be looking for ways to improve the look and readability of their code. One of my favorite ways to reduce vertical length while maintaining readability is to use the lesser-known ternary comparison operator. </p> <p>The Ternary Operator is unique when it comes to PHP's available comparison options. Not in the sense that it does something that the other operators don't, but that the functionality it offers is not seen anywhere else. </p> <p><code><span style="color: #000000;"> <span style="color: #0000bb;">&lt;?php<br />&nbsp;&nbsp;$myvar&nbsp;</span><span style="color: #007700;">=&nbsp;(</span><span style="color: #0000bb;">$x&nbsp;</span><span style="color: #007700;">==&nbsp;</span><span style="color: #0000bb;">$y</span><span style="color: #007700;">)&nbsp;?&nbsp;</span><span style="color: #0000bb;">TRUE&nbsp;</span><span style="color: #007700;">:&nbsp;</span><span style="color: #0000bb;">FALSE</span><span style="color: #007700;">;<br /></span><span style="color: #0000bb;">?&gt;</span> </span> </code> </p> 6/23/2008 12:00pm Caching PHP Pages with Output Buffering http://www.mikebernat.com/blog/Caching_PHP_Pages_with_Output_Buffering <p><img style="border: 1px solid black; background: #ffffff none repeat scroll 0% 0%; float: right; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; padding: 3px;" src="http://www.mikebernat.com/images/php_logo.gif" border="0" alt="PHP" width="70" height="36" /><span>Noticing your pages are loading slowly or just don't like using extra <span>cpu</span> cycles when you don't have to? PHP makes caching very easy with their variety of Output Control Functions. In this article I'll go over complete page caching which is the easiest to implement and understand.</span></p> 6/18/2008 12:00pm Setting-Up a Relational Database in MySQL http://www.mikebernat.com/blog/Setting-Up_a_Relational_Database_in_MySQL <p><img style="border: 1px solid black; padding: 3px; background: #fff; float: right;" src="http://www.mikebernat.com/images/mysql_logo.gif" border="0" alt="MySQL" />Relational Database Design is one of the most powerful ways to ensure data integrity and a great way to kick-off any project. Very often the first thing developers do when starting a new project, or stub-project, is to design the database. This way the structure of the application is already in place and we just have to fill in the pieces with some server-side code. I've found when adding relational constraints to your database design you add in a very powerful error reporting tool that will let you know during the development process that you have allowed something to happen that shouldn't have. In this article, I go through, step by step, showing how to set up a simple relational database and discuss the benefits that are enjoyed.</p> 6/17/2008 10:30am Adding Virtual Hosts to Apache & Wampserver http://www.mikebernat.com/blog/Adding_Virtual_Hosts_to_Apache_&_Wampserver <p><img style="float: right;border: 0px" src="http://www.mikebernat.com/images/wampserver.png" border="0" alt="Wamp Server" width="116" height="96" />Being able to develop on my local machine with urls like htto://testsite/index.php has been a godsend. Mostly because I am able to take full advantage of the document root. If I were to develop on something like http://localhost/testsite/index.php I would be extremely limited and forced to used relative urls. In this article I've listed the steps in order to enable this functionality in a wamp server environment.</p> 6/15/2008 9:00am How fast can you spot PHP Bugs? http://www.mikebernat.com/blog/How_fast_can_you_spot_PHP_Bugs? <p><a href="http://www.phpvs.net/2008/06/04/ten-php-best-practices-tips-that-will-get-you-a-job/">Blake</a> over at <a href="http://www.phpvs.net">PHP vs .NET</a> has written up a very nice article to prepare those who are interviewing for a php job soon. He basically gives a bunch of php snippets and asks where the bugs are. The article somewhat morphs into a mish-mash of good-practice/bad-practice comparison and explains why one way is better than the other.</p> <p>From the article: </p> <p>Find the errors in the following code: </p> <p><code><span style="color: #000000;"> <span style="color: #0000bb;">&lt;?php </span><span style="color: #007700;"><br />function&nbsp;</span><span style="color: #0000bb;">baz</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$y&nbsp;$z</span><span style="color: #007700;">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000bb;">$x&nbsp;</span><span style="color: #007700;">=&nbsp;new&nbsp;Array();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000bb;">$x</span><span style="color: #007700;">[</span><span style="color: #0000bb;">sales</span><span style="color: #007700;">]&nbsp;&nbsp;=&nbsp;</span><span style="color: #0000bb;">60</span><span style="color: #007700;">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000bb;">$x</span><span style="color: #007700;">[</span><span style="color: #0000bb;">profit</span><span style="color: #007700;">]&nbsp;=&nbsp;</span><span style="color: #0000bb;">20</span><span style="color: #007700;">:<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;foreach(</span><span style="color: #0000bb;">$x&nbsp;</span><span style="color: #007700;">as&nbsp;</span><span style="color: #0000bb;">$key&nbsp;</span><span style="color: #007700;">=&nbsp;</span><span style="color: #0000bb;">$value</span><span style="color: #007700;">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000bb;">$key</span><span style="color: #007700;">+</span><span style="color: #dd0000;">"&nbsp;\"+$value+\"&lt;BR&gt;\";<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}&nbsp;<br />?&gt; </span></span></code></p> 6/14/2008 10:00pm MySQL - InnoDB vs MyISAM http://www.mikebernat.com/blog/MySQL_-_InnoDB_vs_MyISAM <p>Most people who use MySQL know that MyISAM and InnoDB are the two most-common database engines available with the popular opensource datbase provider. I would be that most of those people don't even take the time to select a storage-engine and just accept the database default. Those of you who are left probably heard from a friend who saw something online that said one of the two is better than the other. Those of you who are left will still probably learn a thing or two here :)</p> <p>&nbsp;<img style="border: 0px;" src="http://www.mikebernat.com/images/mysql-logo.png" border="0" alt="MySQL" width="200" height="103" /></p> 6/14/2008 7:00pm Image Styles Not Working in TinyMCE? http://www.mikebernat.com/blog/Image_Styles_Not_Working_in_TinyMCE? <p><img style="float: right;" src="http://www.mikebernat.com/images/moxie.gif" border="0" alt="TinyMCE" />I had an issue lately where image styles that I inputted through <a href="http://tinymce.moxiecode.com/">TinyMCE</a> weren't working. For example, my images would not align right or left, my borders weren't appearing, and even padding wasn't working. I checked the HTML source and found that no styles at all were making it from the popup Image Dialog to the editor!</p> 6/14/2008 1:00pm Funny Names for ASCII Symbols http://www.mikebernat.com/blog/Funny_Names_for_ASCII_Symbols <p><a href="http://www.codinghorror.com/blog/archives/001133.html">Coding Horror</a><span> has a pretty amusing article on the infinite number of names programmers give common <span>ascii</span> symbols. $, %, &amp;, |, etc are seen across a lot of languages but somehow they have accumulated more names than the entire <span>ascii</span> library combined.</span></p> <p style="text-align: center;"><img src="http://uploads.screenshot-program.com/upl3018220683.png" border="0" alt="ASCII Names" width="550" height="126" /></p> 6/14/2008 10:00am Optimization - PHP Benchmarks http://www.mikebernat.com/blog/Optimization_-_PHP_Benchmarks <p>A lot of the gripes about PHP as a language come down to the fact that there are a million ways to accomplish the same task, but only a couple are truely 'correct' - as in significantly better for one reason or another. Out of all those reasons script efficiency and speed are the driving force behind all experienced programmers.&nbsp; That being said it's hard to know what the right way to do things are. Sure there are 100 ways to do something but all but 2 or 3 of those take an obscene amount of time. Normally, during development, you won't notice long load times because it's not getting hammered by thousands of users requesting the same page. </p> <p><a href="http://www.chrisvincent.com.au/" target="_blank">Chris Vincent </a>has set up <a href="http://www.phpbench.com/" target="_blank">The PHP Benchmark</a> to help visually show how some methods are more effecient than others. </p> <blockquote>PHPBench.com was constructed as a way to open people's eyes to the fact that not every PHP code snippet will run at the same speed. </blockquote> <p>A few highlights:</p> <p class="code"><code><span style="color: #000000;"> <span style="color: #0000bb;">&lt;?php <br /></span><span style="color: #007700;">foreach(</span><span style="color: #0000bb;">$aHash&nbsp;</span><span style="color: #007700;">as&nbsp;</span><span style="color: #0000bb;">$val</span><span style="color: #007700;">);<br /></span><span style="color: #ff8000;">//&nbsp;VS<br /></span><span style="color: #007700;">while(list(,</span><span style="color: #0000bb;">$val</span><span style="color: #007700;">)&nbsp;=&nbsp;</span><span style="color: #0000bb;">each</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$aHash</span><span style="color: #007700;">)); <br /></span><span style="color: #0000bb;">?&gt;</span> </span> </code> </p> <ul> <li>foreach($array as $value) is the fastest way to loop through an array.</li> <li>foreach($array as $key =&gt; $value) is slightly slower by about 10-15% so use it only when necassary.</li> <li>Using while() and for() loops dramatically increases the server load especially when used in conjuction with list() and each().</li> </ul> 6/10/2008 8:00am Making Your Tables Easier on the Eyes http://www.mikebernat.com/blog/Making_Your_Tables_Easier_on_the_Eyes <p><a href="http://www.richardoneill.com.au">Richard O'Neill</a> has written a short article on improving user-interface by polishing-up your grids and tables. By making a few simple modifications your information can be easily read and understood. </p><blockquote><p><em>&quot;It's not hard making interfaces look professional. I've found that some good looking icons and a bit of cell padding makes all the difference.<br />Using the example below, we can apply small changes to the design which will improve usability and make this ugly interface look a lot better.&quot;</em></p></blockquote><p><img src="http://www.richardoneill.com.au/img/articles/emailclient5.jpg" border="0" alt=" Making Your Tables Easier on the Eyes" width="532" height="254" /> </p> 9/11/2007 8:00am Creating a Users Online Script using PHP http://www.mikebernat.com/blog/Creating_a_Users_Online_Script_using_PHP <p>Creating a way to show how many users are currently visiting your website has become a fad amongst webmasters. It's a cool way to show off your skills has a coder and to show your visitors how many other people are looking at the same thing they are. You don't want to miss the bandwagon again do you? This script is also an excuse to brush up on some mildly advanced MySQL queries.</p> 9/10/2007 8:00am Making Magic with .htaccess Files http://www.mikebernat.com/blog/Making_Magic_with_.htaccess_Files <p>.htaccess files are one of the most underused and underrated forms of web development. They are very easily overlooked or dismissed as unimportant by the amateur programmer. The reality of it is that .htaccess has some of the coolest and ridiculously easy features to offer.</p> 9/9/2007 8:00am Creating a Simple Registration Script using PHP http://www.mikebernat.com/blog/Creating_a_Simple_Registration_Script_using_PHP <p>The most useful and almost necessary feature of any interactive website is a registration form. A way to give users access to features while keeping out the riff-raff. While a registration script can be the perfect time for an amateur to grab the bull by the horns it can also leave lots of pitfalls for a more experienced programmer.</p> 9/8/2007 8:00am Creating Static RSS Feeds http://www.mikebernat.com/blog/Creating_Static_RSS_Feeds <p><img style="border: 0pt none; float: right;" src="images/rss.png" border="0" alt="rss" />Providing RSS Feeds for your website is fast becoming a necessity in today's web2.0 world. The amount of surfers taking advantage of everything RSS Feeds has to offer has exploded in recent history, and it's easy to see why. The amount gained from offering a RSS Feed far outweigh the effort needed to create one.</p> 9/7/2007 8:00am Using PayPal in your Site - Easier than you Think http://www.mikebernat.com/blog/Using_PayPal_in_your_Site_-_Easier_than_you_Think <p><img style="float: right; border: 0px;" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" border="0" alt="PayPal" width="62" height="31" />Integrating PayPal into your site may seem intimidating to those who have never put together a serious project before. It might seem like everything changes when you start dealing with money. You always hear about someone else who has been scammed or in some kind of nightmare involving some sort of online transaction. When you realize how easy PayPal makes it to utilize their services it really puts any anxiety you have to rest.</p> 9/5/2007 8:00am How to Create a Simple PHP Text Counter http://www.mikebernat.com/blog/How_to_Create_a_Simple_PHP_Text_Counter <p>After learning the basics of PHP's basic file system functions, the first thing you'll want to do is put it to use. One of the easiest and flashiest things you can create is a page counting script. I'll show you how to create a page hit script that is easy to create and even easier to implement.</p> 9/4/2007 8:00am PHP Cookies vs Sessions - The Breakdown http://www.mikebernat.com/blog/PHP_Cookies_vs_Sessions_-_The_Breakdown <p>Knowing the basics of cookies and sessions is essential to any successful PHP programmer. It is useful to store pieces information on the users computer for later use. Things like when they laste visited, language of choice, age, etc. Cookies and Sessions are the perfect solution to our needs.</p> <table border="0" cellspacing="0" cellpadding="1"> <tbody> <tr> <td>&nbsp;</td> <td style="background-color: #46647c; border: 1px solid"><strong>Browser needs Cookies Enabled? </strong></td> <td style="background-color: #46647c; border: 1px solid"><strong>Can User Edit Information?</strong></td> <td style="background-color: #46647c; border: 1px solid"> <p><strong>Information Lasts Between Browser Sessions?</strong></p> <p><strong>(Leaving site and coming back)&nbsp;</strong></p> </td> <td style="background-color: #46647c; border: 1px solid"><strong>Information Location&nbsp;</strong></td> </tr> <tr> <td style="background-color: #46647c; border: 1px solid"><strong>Cookies</strong> <br /></td> <td style="background-color: #b3babf; border: 1px solid">&nbsp;Yes</td> <td style="background-color: #b3babf; border: 1px solid">&nbsp;Yes, easily<br /></td> <td style="background-color: #b3babf; border: 1px solid">&nbsp;Yes</td> <td style="background-color: #b3babf; border: 1px solid">&nbsp;User's Browser<br /></td> </tr> <tr> <td style="background-color: #46647c; border: 1px solid"><strong>Sessions</strong><br /></td> <td style="background-color: #c0c0c0; border: 1px solid">&nbsp;No<br /></td> <td style="background-color: #c0c0c0; border: 1px solid">&nbsp;No* </td> <td style="background-color: #c0c0c0; border: 1px solid">&nbsp;No</td> <td style="background-color: #c0c0c0; border: 1px solid">&nbsp;Server, except for session ID<br /></td> </tr> </tbody> </table> 9/3/2007 8:00am Using PHP to Access Files - The Basics http://www.mikebernat.com/blog/Using_PHP_to_Access_Files_-_The_Basics <p>The first thing I wanted to know after learning the basics of PHP was how to store my data on a more permanent basis. PHP is all well and good but without the ability to store data between browser sessions it has limited functionality. After learning how to store and retrieve data, your PHP scripts really come to life.</p> 9/2/2007 8:00am Making your MySQL Tables More Effecient by Using Correct Fieldtypes http://www.mikebernat.com/blog/Making_your_MySQL_Tables_More_Effecient_by_Using_Correct_Fieldtypes <p><span style="font-size: x-small;">One of the biggest mistakes of today's fledgling MySQL programmers is their field type definitions, also known as data type or column type. Many are too quick to set up their tables, flying through the different options without giving it much thought. The time you spend setting up your tables will save you from many headaches later on down the road.</span></p> 9/1/2007 8:00am