The latest version of PHP is optimized for OOP — Object Oriented Programming. Is that a good thing? Certainly, OOP has the center stage in modern programming, and may be an improvement in computer programming. But is it right for PHP?

Let’s go back in our collective history a bit. PHP was created to make it easier for webmasters to integrate server-side functionality into web pages. Combined with MySQL it created a wonderful platform for database driven websites.

So what does any of this have to do with OOP? Nothing. OOP is for programming — writing lower-level programs to do all the wonderful things we can do with computers. Certainly OOP is a step forward for C++ and JAVA and other programming languages, but does it belong in a SCRIPTING environment like PHP? I don’t think so.

Object Oriented Programming uses inheritance and other advanced features to give the programmer the ability to get more work done with less code, and to reuse code more efficiently. But in web scripting, our needs are more modest, and less complicated. Originally most web scripting was done in PERL, but PHP was easier and had as much or more functionality, so it soon replaced PEARL. Now, hosting services are reluctant to upgrade to PHP5 because it has gone too far.

I think PHP5 should have been renamed XPHP for extended php, and branched off as a new programming language — leaving the old PHP scripting language to continue to develop independently, focused on EASE OF USE, rather than technical functionality. PHP was originally designed for Webmaster — not programmers. Let them have their own version of PHP, and leave us with something that makes our life easier, rather than more complex.


How would you like to be able to record hundreds, or thousands of different yes/no values in a single number? This is just one of the many applications where base_convert can come in handy.

Remember basic math classes in high school, where you learned about binary numbers, and hexadecimal, and other number systems that use a different base than our standard base 10?  Remember reading about computers, and how they are ultimately just binary number crunchers? Well let’s put that fund of knowledge to work in our PHP programs.

Suppose you have an on-line questionnaire, with hundreds of questions to which people can reply with a simple yes/no response. You need to record the tens of thousands of results for later analysis. You can do it with a simple integer. In fact, if you have fewer than 64,000 questions (and let’s hope for your subject’s sake that there aren’t that many!) you can use a small integer (SMALLINT) in your mysql database.

How? Well, simply convert each answer into zeros and ones, to create a binary number. Let’s say for example the first five questions were answered ‘yes’ and the next five ‘no’ by a particular subject. You save that as 1111100000. Now use base_convert(1111100000,2,10) and PHP converts the base two number into base 10 for easy storage in your database. Retrieve the number from the database, and convert it back into base two using base_convert(#,10,2). Simple and quick, it saves tons of storage space.


Sometimes you simply want to replace part of string with something else, like changing your address when you move. You could use a fancy grep replace or similar functionality, but if it is a simple substitution it is usually easier to just go with the string function str_replace.

Here is how it works:
$newwork = str_replace($old, $new, $work)

Just set $old to the string of characters you want replaced, $new to the new value you want there (it can be an empty string if you just want to remove something without replacing it), and $work is your original string text. The modified version will be stored in $newwork.


Sometimes you write a function in PHP that needs to know where in the directory hierarchy the code is being called from, to correctly access other files, such as those to be read, written or included. If the same function appears throughout your site, you can’t ‘hard code’ the location, because it changes from one web page to another, if you site spans more than one directory.

PHP, of course, provides a simple solution:

$dir=getcwd();

So, for example, using the above code in a script on your page at mysite.com/widgets/goodstuff.php might return the value:

home/mysite/public_html/widgets

The only cautionary note when using this function, is to remember that it will return the working directory of the calling script, rather than included script, if it is used within an include. So, if your

$dir=getcwd();

is in a file at home/mysite/public_html/includes

And you include it in a script running at home/mysite/public_html/widgets

It will set the $dir variable to that second path, not the path where the included file resides.


The switch() function lets you choose between several options, depending on the value some variable takes. It is similar, for those of you who have programmed in Visual Basic, to the select case command in that language.

So, let’s say you have a feedback form with a drop-down list of typical reasons someone might have for contacting your company. They have to categorize their message as

  1. request for information
  2. order not received
  3. order arrived damaged
  4. other

The form is programmed to assign the number next to each of those reasons to a variable $reason. Depending on the type of message, you want to route the form to different departments:

switch($reason)
{
case 1: $destination="fred@thiscompany.com"; break;
case 2: $destination="mary@thiscompany.com";break;
case 3: $destination="arnold@thiscompany.com";break;
default: $destination="bob@thiscompany.com";
}

Notice we didn’t assign #4 to the last option, but used ‘default’ a catch-all that is executed if none of the other values match. This way, bob will get any messages that do not have the $reason variable set. Also note that each option except the last needs a ‘break’ command at the end, to ensure that subsequent options are not considered. In this case, the default option would execute every time if you forgot the breaks.




Follow

Get every new post delivered to your Inbox.