Netlumination Portfolio
Netlumination Blog

Posts Tagged ‘PHP’


A recursive PHP function to turn digits into words

Wednesday, March 31st, 2010

Warning: This is a big spoiler for Project Euler Problem 17, so if you’re still working on it, don’t keep reading!

I quickly mentioned recursion in my previous post. Turning digits into letters is clearly another example for which recursion is useful. Think about a number like 524. If you know that 4 = “four”, 5 = “five”,  and that 20 = “twenty” then all you have to do, to turn 524 into words,  is run your script on 5, add “hundred,” run your script on 20, and run your script on 4, and if you write your script well, then you can do this using recursion.

Take a look at a working example of the function I wrote to do just this with PHP.

Take a look at my solution to Project Euler Problem 17 [SPOILER ALERT]



Quick Note on Recursion

Monday, March 29th, 2010

Recursion

Example in PHP using the factorial:

  1. // $n must be a positive integer or 0 - function returns 1 for 0 and all errors
  2. function factorial($n)
  3. {
  4.   if ($n > 0 && is_int($n))
  5.   {
  6.     // factorial of positive integer
  7.     return $n * factorial($n - 1);
  8.   } else
  9.   {
  10.     // factorial of 0
  11.     return 1;
  12.   }
  13. }

There are many other blog posts that cover this topic well. Some include:

Recursion in PHP (and iteration)

Recursive patterns in Regex from the PHP manual

Here is a little snippet of a C++ function that will ask for user input and make sure it is a float. If it’s not a float, the functions recurses (is that a word?)… calls itself. The end effect is that the user is asked to enter a float until they do. You must include both iostream and limits.

  1. float collectFloat()
  2. {
  3.   float user_input(0.0);
  4.   try
  5.   {
  6.     // Could also use cin.fail() to check for success
  7.     if (cin >> user_input) cinIgnoreRest();
  8.     else
  9.       throw (string(“\nNot a float entered.\n Please enter a number.\n”));
  10.   }
  11.   catch (string error)
  12.   {
  13.     cout << error << “==> “;
  14.     cin.clear();
  15.     cinIgnoreRest();
  16.     // This will keep repeating until user enters a float
  17.     user_input = collectFloat();
  18.   }
  19.     return user_input;
  20. }


Using Multiple Loops in WordPress with query_posts() and get_categories()

Tuesday, May 5th, 2009

This is a tutorial to demonstrate how to show entire posts from multiple categories in WordPress. This would be especially useful for blogs that contain very short posts like a photo with a short description. This is a very simple problem and solution, but I didn’t find many posts describing how to do it, so I thought it may help a few people out.

In some blogs, it can be useful to show entire posts from multiple categories on one page. For instance for a recipe blog you may want to list the name of each of your recipe categories and the recipes in each of those categories. This would look something like:

  • Breakfast Recipes
    • [Eggs Benedicts Recipe Post]
    • [Pancakes Recipe Post]
  • Lunch Recipes
    • [Grilled Cheese Sandwish Recipe Post]
  • Dinner Recipes
    • [Fettucini Alfredo Recipe Post]
    • [Lasagna Recipe Post]

Read the rest of this entry »



blog