<?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>VerySimple &#187; Languages</title>
	<atom:link href="http://verysimple.com/category/languages/feed/" rel="self" type="application/rss+xml" />
	<link>http://verysimple.com</link>
	<description>Custom Software</description>
	<lastBuildDate>Fri, 06 Jan 2012 18:12:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Install Image Magick for PHP on OSX</title>
		<link>http://verysimple.com/2011/12/21/install-image-magick-for-php-on-osx/</link>
		<comments>http://verysimple.com/2011/12/21/install-image-magick-for-php-on-osx/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 05:34:56 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[OSX]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=1160</guid>
		<description><![CDATA[These instructions will install the ImageMagick binaries as well as the PHP extension for the default OSX Apache/PHP setup.  If you prefer not to use MacPorts there are binaries available, however you may have to deal with some dependencies and configuration on your own. 1. Install ImageMagick sudo port install ImageMagick 2. Install imagic module [...]]]></description>
			<content:encoded><![CDATA[<p>These instructions will install the ImageMagick binaries as well as the PHP extension for the default OSX Apache/PHP setup.  If you prefer not to use MacPorts there are binaries available, however you may have to deal with some dependencies and configuration on your own.</p>
<h3>1. Install ImageMagick</h3>
<p><code>sudo port install ImageMagick</code></p>
<h3>2. Install imagic module</h3>
<p>You&#8217;d expect to just be able to run &#8220;pecl install imagick&#8221; and I would probably try it first.  However the instructions below will work if the automatic installation returns the error: ImageMagick MagickWand API configuration program&#8230; configure: error: not found. Please provide a path to MagickWand-config or Wand-config program. ERROR: &#8216;/private/tmp/pear/temp/imagick/configure &#8211;with-imagick=/opt/local/bin/&#8217; failed.</p>
<p><em>Note version is 3.0.1 in these instructions, adjust the commands depending on the version that is downloaded by pecl.</em></p>
<p><code>pecl download imagick<br />
tar xvzf imagick-3.0.1.tgz<br />
cd imagick-3.0.1<br />
phpize<br />
./configure --with-imagick=/opt/local<br />
make<br />
sudo make install</code></p>
<p>The install process will create a file &#8220;imagick.so&#8221;, but it may not be automatically installed in the correct location depending on your PHP path settings.  The install will output the location of the .so file so if necessary, you can manually move that file to the extensions directory for your web server.</p>
<h3>3. Append to php.ini</h3>
<p><code>extension=imagick.so</code></p>
<p>That should be it.  Restart Apache and view phpinfo.php. There should be a section for ImageMagick</p>
]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2011/12/21/install-image-magick-for-php-on-osx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Four Reasons to NOT use try/catch</title>
		<link>http://verysimple.com/2011/11/14/four-reasons-to-not-use-trycatch/</link>
		<comments>http://verysimple.com/2011/11/14/four-reasons-to-not-use-trycatch/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 20:39:24 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=1111</guid>
		<description><![CDATA[I was reading an argument discussion on google groups regarding the try/catch statement and node.js. In particular there was a suggestion that try/catch is an anti-pattern, meaning a poor development habit that is in common use. I can&#8217;t really agree that try/catch on it&#8217;s own is a bad thing but it is certainly easy to [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading an <span style="text-decoration: line-through;">argument</span> discussion on <a href="https://groups.google.com/forum/#!topic/nodejs/1ESsssIxrUU">google groups</a> regarding the try/catch statement and node.js.  In particular there was a suggestion that try/catch is an anti-pattern, meaning a poor development habit that is in common use.  I can&#8217;t really agree that try/catch on it&#8217;s own is a bad thing but it is certainly easy to abuse. I started thinking about bad code that I&#8217;ve seen due to try/catch and wondered if I could identify concrete examples of when try/catch is the wrong solution to a problem.  I came up with four of them.</p>
<p>Most of these errors I feel are due to developer fear and paranoia that the end user will see an error message.  Nobody wants their customer to encounter bugs and errors, but there is a difference between an app that is solid and one that simply hides problems.  It&#8217;s much harder to fix a bug that nobody knows is there.  I&#8217;ve written about this before in my article about <a href="http://verysimple.com/2009/04/03/insidious-bugs/">insidious bugs</a>.</p>
<h3>1. Sticking Your Head in the Sand:</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// ... code here...</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// hope this never happens!</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A try/catch statement with no error handling inside the catch statement is a great way to mask errors. (masking errors is bad!) Your code quietly suppresses the error and no user or developer will see a problem unless they notice unusual behavior of the app or <strong>data loss</strong>.  This seems to be a strategy used when the code contains an expected error that can be safely ignored and shouldn&#8217;t halt the program. That is a dubious assumption but assuming it is true, the danger with this strategy is that a different, unexpected error might also occur. Because this try/catch doesn&#8217;t discriminate it will mask all errors that occur and will hide a situation that wasn&#8217;t anticipated.</p>
<p>A better approach (assuming the exception that can be safely ignored) might be to first ensure the try/catch surrounds only the most minimal code or line in question.  Then when the error is caught, verify that this is the expected exception and not something else by checking the exception type, id, etc (depending on the language).  It can be helpful to at least write a &#8220;warning&#8221; to the console or a log file so that when another developer is trying to fix your bugs, he/she will have some clues as to what your logic was when you were writing this code.</p>
<h3>2. Attempting to Set Things Right:</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
  i <span style="color: #339933;">=</span> x<span style="color: #339933;">.</span>toUpperCase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// x must be null.  wonder why...?  oh well, let's fix it!</span>
  i <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Handling an exception by ignoring faulty input and setting variables to arbitrary, default values can be dangerous. This has a high probability of creating &#8220;insidious&#8221; bugs because they don&#8217;t always address the cause of the exception and simply try to fix it.  Again, this masks problems and makes apps extremely difficult to debug.</p>
<p>The optimal way to handle this scenario is to prevent the invalid input at it&#8217;s source, before it gets passed into a method or function.  If this method is supposed to gracefully handle bad input then it should do so by checking for legitimate values explicitly instead before using them.  If the input doesn&#8217;t validate then throw an exception.  This is a scary concept to novice and sloppy programmers alike because they are afraid of  creating errors that must then be hunted down and fixed at the source.  However what this really does is to *reveal* bugs in faulty calling code.  Revealing bugs is a good thing.  It can, unfortunately, open a can of worms and a lot of sloppy calling code now becomes your problem!</p>
<h3>3. The Hot Potato:</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// ... code here...</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// whew!  good thing we caught this!</span>
  <span style="color: #b1b100;">throw</span> e<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A useless try/catch that simply re-throws the original exception does nothing to improve the code. This is probably the least harmful mistake because it doesn&#8217;t mask errors.  However the code is worthless and does nothing to help with debugging or make the app more stable.</p>
<p>The solution is to just remove the try/catch and let the original error occur. The end result is the same.</p>
<h3>4. Re-branding Exceptions:</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// ... code here...</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// jason said throwing exceptions is good. i wonder what he meant?</span>
  <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span> e<span style="color: #339933;">.</span>getMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Catching an exception and then throwing a new exception with some of the original info. This example may not mask bugs but it can make the original problem difficult to locate. The stack trace of the original exception probably contains clues about the original source of the error, but you lose all of that information when you create and throw a new exception.</p>
<p>Believe it or not, a good way to handle this is to remove the try/catch. There is one legit reason (in my opinion) to throw a new exception. If you anticipate an exception to occur and 1) you are able to keep the application in a known state and 2) you need the error text to be more friendly or understandable. For example, if you expect a network connection exception to be thrown under normal conditions then you may want to create your own exception that says &#8220;Network Unavailable&#8221; instead of a allowing a cryptic error message to come from a low-level network API. However you do need to be meticulous and add logic to verify that you are catching only the expected exception and not something else. Because if you are expecting a network exception but instead get an &#8220;index out of bounds&#8221; then your &#8220;Network Unavailable&#8221; message is not only masking a bug, but potentially sending another developer on a wild goose chase.</p>
<p>Those are the situations that I was able to think of off the top of my head.  Do you have any other try/catch situations worth mentioning? Feel free to leave them in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2011/11/14/four-reasons-to-not-use-trycatch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing LAMP Stack on OSX 10.7 Lion</title>
		<link>http://verysimple.com/2011/08/18/installing-lamp-stack-on-osx-10-7-lion/</link>
		<comments>http://verysimple.com/2011/08/18/installing-lamp-stack-on-osx-10-7-lion/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 22:14:41 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[OSX]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=998</guid>
		<description><![CDATA[This is a follow-up to my previous article on installing a LAMP Stack on Snow Leopard and explains the steps that you need to get Lion up and running for LAMP development.  (Linux, Apache, MySQL and PHP in case you don&#8217;t already know).  Lion comes pre-installed with Apache and PHP 5.3.6 so you only need [...]]]></description>
			<content:encoded><![CDATA[<p>This is a follow-up to my previous article on <a href="http://verysimple.com/2009/09/18/installing-lamp-stack-on-osx-10-6-snow-leopard/" target="_blank">installing a LAMP Stack on Snow Leopard</a> and explains the steps that you need to get Lion up and running for LAMP development.  (Linux, Apache, MySQL and PHP in case you don&#8217;t already know).  Lion comes pre-installed with Apache and PHP 5.3.6 so you only need to enable those features.  MySQL is the only component that you have to install.</p>
<h3>1.Turn on Apache</h3>
<p>Go to System Settings-&gt;Sharing and turn on Web Sharing. If necessary click the button that says &#8220;Create Home Folder&#8221; which will create a folder called &#8220;Sites&#8221; in your home directory.  You now have a web server that you can access at http://localhost/~username/  (&#8220;username&#8221; being your own account login name)</p>
<h3>2. Edit /private/etc/apache2/httpd.conf</h3>
<p>To enable PHP uncomment (remove the number sign at the beginning of) the line:</p>
<p>#LoadModule php5_module libexec/apache2/libphp5.so</p>
<p>(optional) you can re-map the server root to your home website directory by editing DocumentRoot:</p>
<p>DocumentRoot “/Users/username/Sites”</p>
<p>If you wish to use .htaccess files then look a bit further down AllowOverride and set it to &#8220;All&#8221; like so:</p>
<p>Options FollowSymLinks<br />
AllowOverride All<br />
Order deny,allow<br />
Deny from all</p>
<h3>3. Edit /private/etc/apache2/users/username.conf</h3>
<p>Your user directory has it&#8217;s own permissions as well which are located in this separate file.  You may wish to enable FollowSymLinks and AllowOverride again here by changing these two lines like so:</p>
<p>Options Indexes MultiViews FollowSymLinks<br />
AllowOverride to All</p>
<h3>4. Copy /private/etc/php.ini.default to /private/etc/php.ini</h3>
<p>OSX includes a default php.ini file that you can use but you must rename or copy it first.  If you don&#8217;t do this then PHP will still run but it will just be using the default initialization settings with no way for you to override them.</p>
<p>Edit these settings in <strong>php.ini</strong> (some of these settings will not come into play until they are installed)</p>
<p>display_errors = On<br />
mysql.default_socket = /tmp/mysql.sock<br />
pdo_mysql.default_socket=/tmp/mysql.sock<br />
date.timezone = &#8216;America/Chicago&#8217;<br />
include_path = &#8220;.:/usr/lib/php/pear&#8221;</p>
<h3>5. (Optional) Install PEAR</h3>
<p>If you use the PEAR libraries you can install them using the included phar file like so:</p>
<p>sudo php /usr/lib/php/install-pear-nozlib.phar<br />
sudo pear config-set php_ini /private/etc/php.ini<br />
sudo pecl config-set php_ini /private/etc/php.ini<br />
sudo pear upgrade-all</p>
<h3>6.  Install MySQL</h3>
<p>This doesn&#8217;t really require instructions, simply download the latest MySQL from <a href="http://mysql.com/">mysql.com</a>.  I installed the 32 bit version, however it may be possible to run the 64 bit version as well.</p>
<h3>7. (Optional) Install Apache Plugins</h3>
<p>If you use encryption you may need to install mcrypt.  <a href="http://michaelgracie.com/2011/07/21/plugging-mcrypt-into-php-on-mac-os-x-lion-10-7/" target="_blank">Instructions</a> have been provided by Michael Gracie</p>
<p>If you use the Zend debugger you can install that using <a href="http://verysimple.com/2011/08/16/installing-zend-php-debugger-in-eclipse-on-osx/" target="_blank">these instructions</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2011/08/18/installing-lamp-stack-on-osx-10-7-lion/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Installing Zend PHP Debugger in Eclipse on OSX</title>
		<link>http://verysimple.com/2011/08/16/installing-zend-php-debugger-in-eclipse-on-osx/</link>
		<comments>http://verysimple.com/2011/08/16/installing-zend-php-debugger-in-eclipse-on-osx/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 21:02:08 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[OSX]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=993</guid>
		<description><![CDATA[This walk-through video shows how to install  and enable PHP debugging using the Zend debugger in Eclipse on OSX.  Instead of using print_r and die statements you can step through your code line-by-line, inspect variables and objects, set breakpoints and view the call stack. Using a debugger really pays off in the long run in [...]]]></description>
			<content:encoded><![CDATA[<p>This walk-through video shows how to install  and enable PHP debugging using the Zend debugger in Eclipse on OSX.  Instead of using print_r and die statements you can step through your code line-by-line, inspect variables and objects, set breakpoints and view the call stack.</p>
<p>Using a debugger really pays off in the long run in my opinion.  It gives you so much visibility and insight into what&#8217;s happening in your code and allows you to work faster.  Most developers that I know don&#8217;t bother installing a debugger for PHP web applications because either they either aren&#8217;t aware of how well it works and/or they think the initial setup is a pain.  The setup does have a few steps but actually isn&#8217;t difficult.  This video walks you through all of the steps to install the Zend debugger.</p>
<p><a href="http://verysimple.com/2011/08/16/installing-zend-php-debugger-in-eclipse-on-osx/"><em>Click here to view the embedded video.</em></a></p>
<p><strong>Download links referenced in the video:</strong></p>
<p>Zend Studio Web Debugger (Apache Module):<br />
<a title="http://www.zend.com/en/products/studio/downloads" dir="ltr" href="http://www.zend.com/en/products/studio/downloads" rel="nofollow" target="_blank">http://www.zend.com/en/products/studio/downloads</a></p>
<p>Zend Debugger Plugin Install Site (Eclipse Plugin):<br />
<a title="http://downloads.zend.com/pdt" dir="ltr" href="http://downloads.zend.com/pdt" rel="nofollow" target="_blank">http://downloads.zend.com/pdt</a></p>
<p>Optionally you can download the Zend debugger plugin and install it manually by copying the files to your Eclipse folder instead of using the &#8220;Install Site&#8221;  That can be downloaded here: <a href="http://www.zend.com/en/community/pdt ">http://www.zend.com/en/community/pdt </a></p>
<p><strong>Configuration text for php.ini</strong></p>
<p>zend_extension=/usr/lib/php/extensions/ZendDebugger.so<br />
zend_debugger.expose_remotely=always<br />
zend_debugger.connector_port=10000<br />
zend_debugger.allow_hosts=127.0.0.1/32,192.168.*.*,10.*.*.*</p>
]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2011/08/16/installing-zend-php-debugger-in-eclipse-on-osx/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Evaluating Math in PHP</title>
		<link>http://verysimple.com/2011/06/28/evaluating-math-in-php/</link>
		<comments>http://verysimple.com/2011/06/28/evaluating-math-in-php/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 01:02:38 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=930</guid>
		<description><![CDATA[This is a simple math formula parser for PHP.  This is based off of various comments at php.net.  For security all alphabetical characters are stripped which means that functions like sin, cos, etc are not supported.  Please leave any improvements or suggestions in the comments below. /** * Evaluates a math equation and returns the result, [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple math formula parser for PHP.  This is based off of various comments at <a href="http://php.net/eval" target="_blank">php.net</a>.  For security all alphabetical characters are stripped which means that functions like sin, cos, etc are not supported.  Please leave any improvements or suggestions in the comments below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
* Evaluates a math equation and returns the result, sanitizing
* input for security purposes.  Note, this function does not
* support math functions (sin, cos, etc)
*
* @param string the math equation to evaluate (ex:  100 * 24)
* @return number
*/</span>
<span style="color: #000000; font-weight: bold;">function</span> evalmath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$equation</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// sanitize imput</span>
<span style="color: #000088;">$equation</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[^0-9+\-.*\/()%]/&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$equation</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// convert percentages to decimal</span>
<span style="color: #000088;">$equation</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/([+-])([0-9]{1})(%)/&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;*(1<span style="color: #000099; font-weight: bold;">\$</span>1.0<span style="color: #000099; font-weight: bold;">\$</span>2)&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$equation</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$equation</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/([+-])([0-9]+)(%)/&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;*(1<span style="color: #000099; font-weight: bold;">\$</span>1.<span style="color: #000099; font-weight: bold;">\$</span>2)&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$equation</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$equation</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/([0-9]+)(%)/&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.<span style="color: #000099; font-weight: bold;">\$</span>1&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$equation</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$equation</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;return &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$equation</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;;&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unable to calculate equation&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2011/06/28/evaluating-math-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Update WordPress SimplePie to 1.3-dev</title>
		<link>http://verysimple.com/2011/06/18/update-wordpress-simplepie-to-1-3-dev/</link>
		<comments>http://verysimple.com/2011/06/18/update-wordpress-simplepie-to-1-3-dev/#comments</comments>
		<pubDate>Sat, 18 Jun 2011 21:47:34 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=907</guid>
		<description><![CDATA[WordPress includes an RSS parsing library called SimplePie.  WordPress utilizes this open source project to handle the low-level details of parsing RSS feeds. The version included with WordPress (as of 3.1.3) is 1.2.  However this is not the most recent version of SimplePie.  Attached to this post is SimplePie 1.3-dev which is compatible with version [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress includes an RSS parsing library called <a href="http://simplepie.org" target="_blank">SimplePie</a>.  WordPress utilizes this open source project to handle the low-level details of parsing RSS feeds.</p>
<p>The version included with WordPress (as of 3.1.3) is 1.2.  However this is not the most recent version of SimplePie.  Attached to this post is SimplePie 1.3-dev which is compatible with version 1.2 but addresses various issues.</p>
<p>If you are having trouble parsing feeds, you may try downloading this file and replacing the file /wp-includes/class-simplepie.php with this one.  Be sure to back up your original file in case anything goes wrong.</p>
<p>Click the package below to download the file:</p>
<p><a href="http://verysimple.com/wp-content/uploads/2011/06/simplepie-1.3-dev.zip" title="Download SimplePie 1.3-dev"><img class="alignnone" title="Download SimplePie 1.3-dev" src="http://verysimple.com/wp-includes/images/crystal/archive.png" alt="Download SimplePie 1.3-dev" width="46" height="60" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2011/06/18/update-wordpress-simplepie-to-1-3-dev/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Catching PHP Errors, Warnings and Notices</title>
		<link>http://verysimple.com/2010/11/02/catching-php-errors-warnings-and-notices/</link>
		<comments>http://verysimple.com/2010/11/02/catching-php-errors-warnings-and-notices/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 20:41:06 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=745</guid>
		<description><![CDATA[PHP raises errors in a different way depending on the function or object you are dealing with. This presents a challenge for consistent error handling in an application because errors, unlike Exceptions, cannot be caught with a try/catch statement. This article will explain how to tell PHP to throw Exceptions instead of errors so that [...]]]></description>
			<content:encoded><![CDATA[<p>PHP raises errors in a different way depending on the function or object you are dealing with.  This presents a challenge for consistent error handling in an application because errors, unlike Exceptions, cannot be caught with a try/catch statement.  This article will explain how to tell PHP to throw Exceptions instead of errors so that you can handle them in your code with a try/catch.  The results end up looking like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// ExceptionThrower example</span>
<span style="color: #666666; font-style: italic;">// source for ExceptionThrower is at the bottom of this post</span>
&nbsp;
ExceptionThrower<span style="color: #339933;">::</span><span style="color: #004000;">Start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try 
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;missing/file.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;I HANDLED THIS ERROR: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ex</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
ExceptionThrower<span style="color: #339933;">::</span><span style="color: #004000;">Stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Errors cannot normally be handled with a try/catch.  The PHP compiler takes over, takes the script out of your control and then either directly injects text to the browser and/or halts execution.  PHP allows you to alter this behavior using the <strong>set_error_handler</strong> function, but it seems better as a global error handler and not for catching expected errors in a small block of code.  Most runtime errors, though, can be intercepted and re-thrown as an Exception so that they can be handled consistently.  Unfortunately E_FATAL as well any any error that is raised at compile time cannot be intercepted (such as those thrown by require_once).  However compile-time errors often indicate a syntax error.</p>
<p><span id="more-745"></span></p>
<p>The example below shows how to use a utility class called ExceptionThrower to handle a single function call and then re-set the PHP error handler as it was.  By default this handles all runtime errors except E_DEPRECATED (though PHP earlier than 5.3 doesn&#8217;t define E_DEPRECATED so those errors are caught but ignored).  You can turn this off for even stricter error handling, however a lot of third-party libraries will use deprecated methods and you may not wish to port them all.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// ExceptionThrower example:</span>
&nbsp;
ExceptionThrower<span style="color: #339933;">::</span><span style="color: #004000;">Start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;missing/file.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;I HANDLED THIS ERROR: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ex</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
ExceptionThrower<span style="color: #339933;">::</span><span style="color: #004000;">Stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Utility for catching PHP errors and converting them to an exception
 * that can be caught at runtime
 * @author Jason Hinkle
 * @copyright  1997-2011 VerySimple, Inc.
 * @license    http://www.gnu.org/licenses/lgpl.html  LGPL
 * @version 1.0
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> ExceptionThrower
<span style="color: #009900;">&#123;</span>
&nbsp;
	static <span style="color: #000088;">$IGNORE_DEPRECATED</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Start redirecting PHP errors
	 * @param int $level PHP Error level to catch (Default = E_ALL &amp; ~E_DEPRECATED)
	 */</span>
	static <span style="color: #000000; font-weight: bold;">function</span> Start<span style="color: #009900;">&#40;</span><span style="color: #000088;">$level</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$level</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;E_DEPRECATED&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$level</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">E_ALL</span> <span style="color: #339933;">&amp;</span> ~E_DEPRECATED <span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// php 5.2 and earlier don't support E_DEPRECATED</span>
				<span style="color: #000088;">$level</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$IGNORE_DEPRECATED</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">set_error_handler</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ExceptionThrower&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;HandleError&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$level</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Stop redirecting PHP errors
	 */</span>
	static <span style="color: #000000; font-weight: bold;">function</span> Stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">restore_error_handler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Fired by the PHP error handler function.  Calling this function will
	 * always throw an exception unless error_reporting == 0.  If the
	 * PHP command is called with @ preceeding it, then it will be ignored
	 * here as well.
	 *
	 * @param string $code
	 * @param string $string
	 * @param string $file
	 * @param string $line
	 * @param string $context
	 */</span>
	static <span style="color: #000000; font-weight: bold;">function</span> HandleError<span style="color: #009900;">&#40;</span><span style="color: #000088;">$code</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// ignore supressed errors</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$IGNORE_DEPRECATED</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;deprecated&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #000088;">$code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can alternatively call <strong>ExceptionThrower::Start()</strong> at the beginning of your script and never call <strong>ExceptionThrower::Stop()</strong> This would cause all PHP errors to be converted into Exceptions for the duration of the script.  If you don&#8217;t have any other error handling, I think this is a decent approach.  As a warning, though even E_NOTICE errors will cause execution to halt.  That&#8217;s not necessarily a bad thing in my opinion because it may lead you to find bugs that you didn&#8217;t know existed.</p>
<p>Runtime errors don&#8217;t necessarily indicate a bug.  They can be caused by anything from bad user input to a database connection issue, missing libraries, etc.  They could be a bug, but they may simply be a known state that can be handled.  Runtime Errors come in a variety of flavors such as E_ERROR, E_WARNING, E_NOTICE and <a href="http://php.net/manual/en/errorfunc.constants.php">others</a>.  Depending on your server configuration you may see them in your browser output or error logs.  In most cases they halt the execution of your script and lead to half-rendered pages or blank pages.</p>
<p>Be aware that only one function can assigned as the error handler at a time, so if any other libraries call <strong>set_error_handler</strong>, they will take over the error handling for the entire app.  When you call <strong>restore_error_handler</strong>, PHP will set it back to whatever it was previously.  In other words it is a last-in, first-out (<a href="http://en.wikipedia.org/wiki/LIFO_(computing)">LIFO</a>) stack.</p>
<p>Please don&#8217;t interpret this article as encouragement to suppress PHP errors, mask bugs or allow your code to go into an unknown state.  The point here is exactly the opposite of that.  It is to prevent a PHP runtime error from halting your script and/or barfing all over your page with warnings.  The goal is to catch expected runtime errors in order to keep the application in a known state, close open resources, rollback transactions, log the error, render a decent error page, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2010/11/02/catching-php-errors-warnings-and-notices/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>HungryFEED RSS Plugin for WordPress</title>
		<link>http://verysimple.com/2010/07/22/hungryfeed-rss-plugin-for-wordpress/</link>
		<comments>http://verysimple.com/2010/07/22/hungryfeed-rss-plugin-for-wordpress/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 05:53:35 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=702</guid>
		<description><![CDATA[I decided to delve into the world of WordPress plugin development this week.  I&#8217;m a contributor to a site for film camera gear and we have a page that lists equipment for sale.  We wanted to pull in an RSS feed from eBay and display it on a page.  There seems to be a WordPress [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to delve into the world of WordPress plugin development this week.  I&#8217;m a contributor to a site for film camera gear and we have a page that lists equipment for sale.  We wanted to pull in an RSS feed from eBay and display it on a page.  There seems to be a WordPress plugin for anything, but I was surprised that I couldn&#8217;t find one that worked the way I liked.  Anyway, two hours later and I had managed to write my own.  Without further ado, here&#8217;s <strong><a href="/products/hungryfeed/">HungryFEED</a></strong>.</p>
<p>I&#8217;m really impressed with the WordPress plugin API and distribution system.  It&#8217;s well organized with great documentation.  I was psyched to see the little &#8220;upgrade available&#8221; button next to my own plugin once I published it to the <a href="http://wordpress.org/extend/plugins/">WordPress plugin directory</a>!  Even though I prefer object-oriented code, I have to admit the procedural style of WordPress code is consistent and easy to follow.</p>
<p>In the process I also had get my hands dirty with <a href="http://simplepie.org/">SimplePie</a> RSS library that WordPress uses.  It does a nice job of parsing all kinds of RSS but I found it a bit difficult to debug feed errors.  Its also a little touchy with eBay feeds and there seems to be some glitches using set_feed_url(), so I would up accessing feed_url directly, which doesn&#8217;t seem right.  But it works so it&#8217;ll have to do for now.</p>
<p>If you give the plugin a try, feel free to leave a comment on directly the HungryFEED page.</p>
]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2010/07/22/hungryfeed-rss-plugin-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Phreeze Running as a Joomla Component</title>
		<link>http://verysimple.com/2010/07/10/phreeze-running-as-a-joomla-component/</link>
		<comments>http://verysimple.com/2010/07/10/phreeze-running-as-a-joomla-component/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 05:37:39 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=579</guid>
		<description><![CDATA[Our company has decided on Joomla to manage our primary website, however we&#8217;ve been writing code using our own Phreeze framework for several years and have a lot of time invested in these applications.  We&#8217;re now trying to integrate all of our web properties and provide single sign-on, which is a challenging task.  It occurred [...]]]></description>
			<content:encoded><![CDATA[<p>Our company has decided on <a href="http://www.joomla.org/" target="_blank">Joomla</a> to manage our primary website, however we&#8217;ve been writing code using our own <a href="http://www.phreeze.com" target="_blank">Phreeze</a> framework for several years and have a lot of time invested in these applications.  We&#8217;re now trying to integrate all of our web properties and provide single sign-on, which is a challenging task.  It occurred to me that we might be able to run our Phreeze applications as a plugin within the Joomla framework to avoid having to re-write a lot of code.</p>
<p><span id="more-579"></span><a href="http://verysimple.com/wp-content/uploads/2010/07/poc.png" rel="shadowbox[sbpost-579];player=img;" title="Proof of Concept"><img class="alignright size-medium wp-image-580" title="Proof of Concept" src="http://verysimple.com/wp-content/uploads/2010/07/poc-300x242.png" alt="" width="300" height="242" /></a>The solution so far is to write a custom authentication plugin plus a component for Joomla.  The plugin handles sign-on and the component allows us to render basically anything within the content area of the Joomla site.  I was able to get proof-of-concept running today and not only does the Phreeze code render correctly inside the Joomla system, it wasn&#8217;t even necessary to merge the Phreeze code, it runs just fine with the source code in a separate folder from Joomla.  The component just has to do a little magic and configure some paths to make Phreeze happy.</p>
<p>This seems to be one of those rare moments where strict coding practices actually pays off.  I designed Phreeze to be an extremely modular MVC / ORM with the idea that we might one day use another rendering engine, or switch data engines.  We&#8217;re even in the process of swapping out direct database connection with REST services with no change to the views, which will be exciting.  Dependency injection is used for all the various parts so you can swap anything.  But I hadn&#8217;t really considered running the entire framework within another system.  So I was somewhat surprised when the code just ran with almost no configuration.</p>
<p>The biggest problem is with relative URLs.  Joomla I must say is pretty great about passing control to a component and then staying out of the way, even if you throw lots of weird querystring parameters on the end of the URL.  However, the relative paths to scripts, styles, images, etc are proving to be problematic.</p>
<p>I haven&#8217;t decided on the best approach yet but I&#8217;m considering simply writing new Views for Joomla.  This allows us to re-use all of our Model and Controller code.  I&#8217;d prefer to just use everything exactly as-is in each application but in some ways it might make more sense for the apps to render themselves differently when inside the Joomla system vs when running stand-alone.</p>
<p>Phreeze has a URL writer class which does help to solve the relative URL problem, however we didn&#8217;t use it consistently in all the views and so there is some cleanup to do.  Also this class isn&#8217;t accessible from within static JavaScript code, so that might get messy with some of the more advanced pages that make a lot of Ajax calls.</p>
<p>This is possibly my nerdiest post ever but it&#8217;s exciting to spend so much effort at writing adaptable code and then finally the day comes where you get to see it work in a totally unexpected way.</p>
]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2010/07/10/phreeze-running-as-a-joomla-component/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install WordPress 3 with Multiple Domains</title>
		<link>http://verysimple.com/2010/07/09/install-wordpress-3-with-multiple-tlds/</link>
		<comments>http://verysimple.com/2010/07/09/install-wordpress-3-with-multiple-tlds/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 05:27:12 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://verysimple.com/?p=560</guid>
		<description><![CDATA[WordPress 3 now includes the features that were previously available as WordPress MU.  &#8220;MU&#8221; stands for Multi-User (or Multi-Site) and this lets you to run multiple blogs all from a single WordPress installation.  This walk-through will help you configure WordPress to manage multiple sites, each with their own second-level domain (SLD). As you go through [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress 3 now includes the features that were previously available as WordPress MU.  &#8220;MU&#8221; stands for Multi-User (or Multi-Site) and this lets you to run multiple blogs all from a single WordPress installation.  This walk-through will help you configure WordPress to manage multiple sites, each with their own <strong>second-level domain</strong> (SLD).</p>
<p>As you go through this walk-through, it helps to understand that WordPress MU was originally designed to create a branded, automated community of blogs using <strong>sub-domains </strong>(i.e. third-level domains) such as user1.wordpress.com, user2.wordpress.com, etc.  You can sign up on wordpress.com right now and get your own instant blog, hosted on their servers.  You become a part of the wordpress.com community.  WordPress MU was designed to allow you to create your own communities under your own domain name branding.</p>
<p>This walk-through addresses a slightly different type of user which is perhaps a server admin or simply a person who runs several blogs.  There is no &#8220;community&#8221; and you don&#8217;t allow anonymous users to stop by and create a blog.  You might currently have WordPress installations on multiple servers with totally unrelated domains.  Keeping the code and plugins updated for multiple blogs gets to be a hassle, so the idea of a single, centralized WordPress install is appealing for maintenance reasons.  Thanks to WordPress&#8217;s flexible architecture you can do this, but it&#8217;s not part of the original design.</p>
<p>The reason I write all of this is to hopefully explain why SLDs are outside the scope of  WordPress&#8217;s original design.  You can still set it up without understanding any of this, however it will make things easier if you know how the pieces fit together.</p>
<p><span id="more-560"></span></p>
<h2>Requirements</h2>
<p>To keep this simple I&#8217;m only going to write about the multi-site configuration.  I&#8217;m assuming you know how to install WordPress, edit config files on your server, create a MySQL database on your host and map sub-domains.  These things may be done differently depending on your host so you will need to do the research for your own host server.  If you run your own server you may have to know how to configure it for multiple domains as well.</p>
<h2>DNS Configuration</h2>
<p>Again, I&#8217;m not going to get into DNS configuration.  However, I will say that a simple setup is to simply point all of your domains to the same IP address and let WordPress do the rest.  You can also assign one domain an IP and then park each additional domain onto this one, which may save hosting fees.  WordPress relies on the hostname, so it doesn&#8217;t matter how you&#8217;ve configured Apache or IP addresses, so long as the domain maps to the physical location of your WordPress install.</p>
<p><em>As a handy tip &#8211; you can edit your local hosts  file to point a domain to the IP of your WordPress installation and WordPress will install easily this  way.  This is a great way to experiment and migrate over an existing site without taking the domain name offline.  If you don&#8217;t understand what this means, then don&#8217;t worry about  it!</em></p>
<h2>1. Install WordPress</h2>
<p>Install using all defaults is fine.  You should install this onto the domain that you plan to use as your &#8220;master&#8221; domain.  My recommendation is to install it at the top level of your domain, for example &#8220;domain.com&#8221; (without the &#8220;www&#8221; prefix).  If you plan to use sub-domains for different sites then this will keep things simple.  You can always add &#8220;www&#8221; during site configuration, but you can&#8217;t remove it from your sub-domain sites if it was installed that way.  For example your sites will look like this: site1.www.domain.com  instead of simply site1.domain.com.</p>
<p>Verify that WordPress is running but don&#8217;t bother putting any content in at this point.  In the next few steps we&#8217;re going to enable new menu items one after the next that are necessary to use the multi-site feature.</p>
<h2>2.Enable the WordPress Multi-Site Feature</h2>
<p><a href="http://verysimple.com/wp-content/uploads/2010/07/ss1.png" rel="shadowbox[sbpost-560];player=img;" title="ss1"><img class="alignright size-full wp-image-561" title="ss1" src="http://verysimple.com/wp-content/uploads/2010/07/ss1.png" alt="" width="160" height="139" /></a>Assuming WordPress is running, edit <strong>wp-config.php</strong> and add the following line:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'WP_ALLOW_MULTISITE'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>After you add this line and save the file, return to the WordPress Admin page and look for a new menu item <strong>Tools-&gt;Network</strong>.  (If on 3.1 then instead you will see a new link next to the Logout link called &#8220;<strong>Network Admin</strong>&#8221; and from there go to <strong>Settings -&gt; Network Setup</strong>) Navigate to this new menu and you&#8217;ll be prompted to &#8220;Create a Network&#8221; in order to enable the multi-site feature.</p>
<p>You have some choices as to whether you want to use sub-domains or sub-folders for your network.  Leave all the defaults and choose sub-domains.  We&#8217;re going to be using separate SLDs anyway so choosing sub-domains seems to make more sense to me.  If you choose folders, you&#8217;ll be on your own from this point forward.</p>
<p><a href="http://verysimple.com/wp-content/uploads/2010/07/ss2.png" rel="shadowbox[sbpost-560];player=img;" title="ss2"><img class="alignright size-full wp-image-562" title="ss2" src="http://verysimple.com/wp-content/uploads/2010/07/ss2.png" alt="" width="158" height="104" /></a>Once you enable the network, you will be given configuration text that you must copy and paste into an <strong>.htaccess</strong> file and some additional variables to be pasted into <strong>wp-config.php</strong>.  Do this as directed on the Network page.</p>
<p>In WordPress 3.0 &#8211; after you edit these files and re-login you should see another new menu &#8220;<strong>Super Admin</strong>&#8221; which is above the dashboard menu.  Your site is now ready for multiple sites.</p>
<p><a href="http://verysimple.com/wp-content/uploads/2010/07/network-admin.png" rel="shadowbox[sbpost-560];player=img;" title="network-admin"><img class="alignright size-full wp-image-879" title="network-admin" src="http://verysimple.com/wp-content/uploads/2010/07/network-admin.png" alt="" width="148" height="80" /></a><strong>In WordPress 3.1 </strong> The Super Admin menu has been replaced with a separate &#8220;<strong>Network Admin</strong>&#8221; screen which you use instead of the &#8220;Super Admin&#8221; menu.  The link to this is located in the upper-right part of the screen next to the &#8220;Log Out&#8221; link.  This link will let you toggle in/out of network admin mode.</p>
<h2>3. Install Plugin WordPress MU Domain Mapping</h2>
<p>At this point your server is already setup to handle multiple sites, but we need to install a plugin in order to support SLDs.  The plugin to use is called &#8220;<a href="http://wordpress.org/extend/plugins/wordpress-mu-domain-mapping/">wordpress-mu-domain-mapping</a>&#8220;  This plugin also includes a special file known as a &#8220;drop-in&#8221; which must be manually copied and hooks into the lower levels of the WordPress API.</p>
<p>You can install this plugin by manually uploading it, or through &#8220;Plugins-&gt;Add New.&#8221;  Just make sure<br />
you install version .052 or higher.   After you have installed it, go ahead and activate the plugin.  You&#8217;ll notice that plugins now have an option to &#8220;<strong>Network Activate</strong>&#8221; which activates a plugin for all sites.</p>
<p><a href="http://verysimple.com/wp-content/uploads/2010/07/ss3.png" rel="shadowbox[sbpost-560];player=img;" title="ss3"><img class="alignright size-full wp-image-563" title="ss3" src="http://verysimple.com/wp-content/uploads/2010/07/ss3.png" alt="" width="157" height="221" /></a>Once the plugin is installed, you need to manually install the Sunrise &#8220;drop-in&#8221;  which is included with the plugin source files.  You may either copy <strong>sunrise.php</strong> from <strong>/</strong><strong>wp-content/</strong><strong>plugins/wordpress-mu-domain-mapping/</strong> to <strong>/wp-content/</strong> or if you prefer  you can create a symbolic link (for example <em>ln -s plugins/wordpress-mu-domain-mapping/sunrise.php</em>.  I prefer the link myself).</p>
<p>Once you&#8217;ve copied or linked the file, you have to edit <strong>wp-config.php</strong> one last time and add the following line which will tell WordPress to activate the drop-in:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'SUNRISE'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'on'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Once you re-load the console you&#8217;ll once again have some new menu items.  This time they appear under the Super Admin (Network Admin in 3.1 or higher) menu and they are <strong>Domain Mapping</strong> and <strong>Domains</strong>.  The Domain Mapping has some settings that don&#8217;t actually need to be changed.  The Domains page is where we&#8217;ll map SLDs to a site (in the next step).</p>
<h2>4. Create a Second Site</h2>
<p>At this point your server is fully ready to support SLD sites, you just need to know how to add them.  The basic concept is that you create a site first using WordPress&#8217;s normal functionality.  Then you using the domain mapping plugin to map the SLD to the new site based on it&#8217;s Id.  Because WordPress doesn&#8217;t yet natively support SLDs, you have to do a little fudging with the forms.</p>
<p>First, under <strong>Super Admin</strong> (or <strong>Network Admin</strong>) go to the <strong>Sites</strong> page.  It is slightly confusing because you have to choose a sub-domain for your site, even though we&#8217;re going to use a SLD.  That&#8217;s ok, just enter whatever you want as the sub-domain and add the site.</p>
<p>Now that you&#8217;ve added the site, you can click &#8220;Edit&#8221; under the new site and this will bring up a long page with lots of settings.  Don&#8217;t worry about changing everything right now but you do want to change these two fields: <strong>Domain</strong> and <strong>Fileupload URL</strong>.  These need to be changed to your new SLD:</p>
<p><a href="http://verysimple.com/wp-content/uploads/2010/07/ss5.png" rel="shadowbox[sbpost-560];player=img;" title="ss5"><img class="alignnone size-thumbnail wp-image-565" title="ss5" src="http://verysimple.com/wp-content/uploads/2010/07/ss5-150x72.png" alt="" width="150" height="72" /></a> <a href="http://verysimple.com/wp-content/uploads/2010/07/ss6.png" rel="shadowbox[sbpost-560];player=img;" title="ss6"><img class="alignnone size-thumbnail wp-image-566" title="ss6" src="http://verysimple.com/wp-content/uploads/2010/07/ss6-150x150.png" alt="" width="72" height="72" /></a> <a href="http://verysimple.com/wp-content/uploads/2010/07/ss7.png" rel="shadowbox[sbpost-560];player=img;" title="ss7"><img class="alignnone size-medium wp-image-567" title="ss7" src="http://verysimple.com/wp-content/uploads/2010/07/ss7-300x38.png" alt="" width="240" height="30" /></a></p>
<p>At some point once WordPress natively supports SLDs you would probably be done at this point.  In fact you will be able to visit your new site if your domain is already configured, but you won&#8217;t be able to log into the admin panel for the site.  So we need to map the domain over to the new site.</p>
<p><em>Before you leave the Sites page, make a note of the ID of your new site.  (In the screenshot above, my site &#8220;whatever&#8221; has the ID of &#8220;8&#8243;).  If it&#8217;s a new install, then your ID will probably be &#8220;2&#8243;.</em></p>
<p><a href="http://verysimple.com/wp-content/uploads/2010/07/ss8.png" rel="shadowbox[sbpost-560];player=img;" title="ss8"><img class="alignright size-medium wp-image-568" title="ss8" src="http://verysimple.com/wp-content/uploads/2010/07/ss8-300x141.png" alt="" width="180" height="85" /></a>Head to the <strong>Super Admin -&gt; Domains</strong> page (in 3.1 this is <strong>Network Admin -&gt; Settings -&gt; Domains</strong>) and there is a form at the bottom to add a new Domain.  Leave the Primary checkbox checked.  Enter your site ID from above and the domain.  Click Save to add the mapping.</p>
<p>At this point you&#8217;re done and you should be able visit your site using your SLD as well as login to the admin panel.  I hope this tutorial worked for you and helped you to understand how WordPress MU feature works as well as some of it&#8217;s current shortcomings.</p>
<p>Please leave a comment if you have any suggestions to make this walk-through easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://verysimple.com/2010/07/09/install-wordpress-3-with-multiple-tlds/feed/</wfw:commentRss>
		<slash:comments>60</slash:comments>
		</item>
	</channel>
</rss>

