Marketing 101. Consulting 101. PHP Consulting. Random geeky stuff. I Blog Therefore I Am.


The FuzzyBlog!

December 2002
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Nov   Jan

Updated: 1/1/2003; 9:12:21 AM.
Search

My Newest Product!

  • Makes email not suck!
  • Works with Outlook
  • Eliminates Spam
  • Color Codes Your Email

Appreciation

Give me a Gift

Amazon Honor System Click Here to Pay Learn More

Books I've Written







Marketing

 Thursday, December 19, 2002

Variable Variables in PHP

Well since it's late but I'm still grinding and I feel guilty for not blogging much today, here's a special PHP Treat: A Variable Variable Tutorial.  Yes that's right.  There is a feature in PHP called "Variable Variables".

One of the issues that I had with implementing our Digibuy support was our old friend and enemy: Parsing.  This is such a fundamental thing but there always seems to be a new wrinkle in it.  In this case we had an existing PHP script which did our registration calculations and was expecting a certain set of variables.  Now this code was fairly complex and I didn't want to re-write it (much).  Here's the input data we had to parse:

author_id=BillBrown & author_password=HemosRules & prod_sku=98079949999

With the exception that it had like 30 odd variables in it.  Since my code was expecting a series of variables I needed to parse this and automatically create a series of variables like $author_id and $author_password.  Now if this data was being given to me by a URL then it would be easy -- PHP could automatically do it or I could just pull them out of $_REQUEST.  But, instead, I had them going to me as if they were a file.  So ... How do you create a series of variables on the fly? 

Note: Yes I could have made an associative array but that wouldn't have taught me anything, now would it ?  I could also have gotten a similar effect with the extract function but I found out about that after this working.  And if the code is working then ...

Well deep in the recesses of what I call a brain was a recollection of "variable variables".  I think I heard about this in a talk that Rasmus gave although I can't honestly be sure.  I do know that whenever you need to do "meta" type tasks like this in a language, you need to poke around the oddball features since that's usually where they reside.  What I do is look for the sections of the documentation that I've never read.  The idea behind a variable variable is simple: interpret the value of a variable and make it into a variable itself.  I.e. the "variable variable" nomenclature. 

Since php uses $ for indicating a variable they've chosen to use $$ to make a variable variable.  Let's say you have two variables, $part1 and $part2.  If $part1 = "author_id" and $part2="HemosRules" how do I get to $author_id = "HemosRules".  Simple: $$part1 = $part2.  Yup.  It's just that easy.  Of course I did need to wrap it into a string parsing loop.  Here's the guts of it:

  $strarray = explode("&",$input);
  foreach ($strarray as $stritem) {
    $stritem = trim($stritem);
    $part1 = substr ( $stritem, 0, strpos ( $stritem,"=" ) );
    $part2 = substr ( $stritem, strpos ( $stritem,"=" )+1, strlen ( $stritem ) );
    #magic!  $$ is a "variable variable" i.e. it converts the value
    #in the variable into a variable itself
    $$part1 = $part2;
  }

The way this works is an input string, $input, contains everything that needs to be processed.  First I explode this into an array using "&" as the delimiter.  Then I loop over the array with a foreach loop creating a $part1 variable and a $part2 variable.  Finally I just do the magic $$part1 = $part2. 

Here's the example Source Code.

Here's the working Example

Thanks again to my favorite Systems Administrator, Apokalyptik, for showing me how to use PHP to generate code listings.  Much easier than pasting it into a blog entry.

More on Variable Variables from PHP.Net: [_Go_]

Variable Variables.  A little confusing but definitely a frothy good thing!


9:16:34 PM      Google It!   comment []    IM Me About This   

I'm Back -- And You Can Now Buy Inbox Buddy Too !!!

Phew!  Well I just finished knocking out the ecommerce stuff for Inbox Buddy and integrating our custom serial number / registration code with the stuff from DigiBuy so you can now buy Inbox Buddy.  It was one of those tedious processes where you are dealing with server to server communications and not seeing the error messages so there was quite a bit of "debugging by guessing and head scratching".  There was also a wee bit of cursing and then the final celebration with Oreo Chip ice cream from J.P. Licks in Newton.

Debugging Tip: When you don't have browser error messages to let you know what's going on, it's really easy to just setup a database table with a TEXT field and use an INSERT statement to route all data into it for inspection.  That finally let me straighten out the issues.  Could I have just logged stuff to a text file?  Sure but I'm increasingly using a database table for logging / debugging stuff since I'm thinking forward for when I'm working with clusters of machines.  In that circumstance, particularly when you have a load balancer, its much easier to use a database table than worrying about which machine is writing to which file.

DigiBuy is an ecommerce provider for small software companies and web sites that lets you easily sell stuff online.  Although the percentage they take is fairly large (13.9%) they seem to do a good job, I've spoken with other users who recommend them and while the setup wasn't seamless, it was pretty good (more on that below).  Although I haven't used them commercially yet, I do like what I see.  Recommended.

So if you are using Inbox Buddy and you last downloaded it on November 20th then your free evaluation month runs out tomorrow -- and Inbox Buddy will stop working.  If you want to purchase a copy then use the link below:

If You Care: Click Here to Buy Inbox Buddy

I'd also like to thank the very nice people at DigiBuy for their help and assistance.  I mentioned that the setup wasn't seamless but that it was pretty good.  My problems with the setup basically involved not reading the documentation well enough (DOH!) and an example that wasn't complete.  My thanks to Paul Folwell and Mike Thompson for getting me straightened out.  Mike was particularly a star and went beyond the call of duty in helping out.

Note: For those that don't know what Inbox Buddy is, see our web site.  In short Inbox Buddy is an email enhancement for Outlook 2000 and Outlook XP which provides anti-spam and email management features.

Oh and in closing, if you need to sell something online in 2003 you might want to setup a Digibuy account during the month of December.  They have a promotion for new vendors where they'll upgrade your initial account ($29.95) to an advanced account which is highly customizable ($199.95 normally) for free.  That's a damn good deal.

Oh and if you need help setting up a Digibuy site we do know our way around the issues now.  At their request, we're going to be furnishing our code framework back to Digibuy as an example.


8:48:52 PM      Google It!   comment []    IM Me About This   

Scripting languages have more options than bisexuals ...

Sometimes you see a line of text on the web that you just have to quote:

2. Scripting languages have more options than bisexuals because they have polymorphic types [_Go_]

An excellent article about the advantages of scripting languages for development.  One benefit that is left out is the pretty much automatic memory management that scripting languages give you.  That's just plain huge.


3:56:10 PM      Google It!   comment []    IM Me About This   

No Real Postings Until this Afternoon

Worked late, slept late, running behind.  And I have some code that has a kind of drop dead date on it.  Sorry. 


8:43:06 AM      Google It!   comment []    IM Me About This