Running PHP4 and 5 together

Runnigng PHP4 and PHP5 together using apache proxying.

To solve this problem, I used Apache’s mod_proxy module and two apache servers… My primary server runs PHP 5 as an Apache module and a second apache server runs PHP 4.3.5 as a module. My configuration requires that you can setup multiple virtual servers and run Apache to listen on localhost on port 8080. Here’s what I did:

Web Data Analysis with PHP

An interesting article on IBMP’s developer works site touches on how to Take Web data analysis to the next level with PHP.

I was expecting an article geared toward getting more interesting metrics out of site access logs but the article focused on using chi-square tests with nominal data like survey results. This article was interesting but I think its less valuable for most web developers and site maintainers who are not using the web to collect experimental data but do want to get better stats on who’s accessing their sites and how the site is being used.

Zend Tutorial on PHP function Libraries

If you’re using function libraries to organize your php code, I highly recommend reading Writing Libraries in PHP on the Zend site. There are a number of very good recommendations as well as insightful discussion of the different approaches available when writing php functions. The author also recommend wrapping you libraries as functions, as I did here. However, he calls his functions as public method calls to an existing object instead of using static calls to a class namespace. No big difference, of course I prefer the later since its more readable.

php function junction

Working well with php functions

Most of the time its very tempting to open up your text editor and start writing code to scratch some itch. Smart programmers know to use functions to wrap up repetitive tasks and make life easier on themselves. The following piece is meant to give your functions a little extra edge both during development and months later when you come back to fix or improve your php script.

Give your functions some class

Sooner or later, you’ll build up libraries of functions that you use quite often. Each library could be its own separate file that you include() or require() at the beginning of the script. For example, when you are working with webpages you could have a library that makes outputting different kinds of lists (as list items, radio buttons, checkboxes, and so on). A second library might clean up strings and display them to the output buffer. These libraries might look a little like this:

--Lists.php--<br />function display() {<br />/* code to display an array as a series of &lt;li&gt; tags */<br />}<br /><br />function radio() {<br />/* code to display array as a series of radio buttons */<br />}<br /><br />function select() {<br />/* code to display an array as a series of select options */<br />}<br /><br />--Output.php--<br />function display() {<br />/* code to run a string through trim, strip_tags, html_entities <br />   and display it. */<br />}<br /><br />function USphone() {<br />/* function to run a numeric string and output as a us phone in<br />   format (999)888-7777 <br />*/<br />}<br /><br />function radio() {<br />/* function to run a string and output the first four letters<br />   in all caps */<br />}<br />

Clearly, the above function signatures are for example only but there are some apparent limitations to organizing function libraries this way. Since all functions exist in the global namespace, you have to keep track of what function names have already been used. This may be managable for a couple of include files but once you are working with more than a half a dozen or so it becomes tedious to inspect them all to make sure your new function name isn’t taken. If you include both files in one script, PHP will produce an error when you try to define an already exisiting function.Finally, when you are maintaining code, it can be a pain to have to figure out which radio() or display() function you are calling in a script.
A simple solution to the above limitations is to wrap your function library as a class as shown below.

--Lists.php--<br />class Lists {<br />    function display() {<br />    /* code to display an array as a series of &lt;br&gt; tags */<br />    }<br /><br />    function radio() {<br />    /* code to display array as a series of radio buttons */<br />    }<br /><br />    function select() {<br />    /* code to display an array as a series of select options */<br />    }<br />} // end of class Lists<br />--Output.php--<br />class Output {<br />    function display() {<br />    /* code to run a string through trim, strip_tags, html_entities <br />       and display it. */<br />    }<br /><br />    function USphone() {<br />    /* function to run a numeric string and output as a us phone in<br />       format (999)888-7777 <br />    */<br />    }<br /><br />    function radio() {<br />    /* function to run a string and output the first four letters<br />       in all caps */<br />    }<br />} // end of class Output<br />

Each function library is now safely cocooned in its own class namespace. The functions are then called using static calls like so:

// output station's name and phone number<br />    echo Output::radio($letters).'&lt;br&gt;';<br />    echo Output::USphone($phone).'&lt;hr&gt;';<br />//output radio options of deejay names<br />    echo List::radio( $deejays );<br />

Its a little more typing but now its obvious what class and file the function is in if you ever need to modify it. Also, when writing new functions you don’t have to worry if the name of your function is already in use.

Other function tips

  1. Don’t solve a problem in the middle of outputting a page or tackling another task – make it a function. Even if its only used on that page.
  2. If you are copying and pasting lines from one place to another and changing some variables around, its should be a function.
  3. After writing a function, see if you can abstract it a little more. Chances are you wil have to solve a similar problem later.
  4. Instead of echoing any output, use php’s output buffering functions to capture the output and return it as a string.
  5. Don’t rely on globals in your function. If your function needs it, it should be passed to it.

Other references