Math For Kids

I just created a couple of PHP scripts for my nephew to help him with his math. Actually, any kid or adult can use it. The program performs some basic mathematics such as addition, subtraction, multiplication and division. The numbers and result are totally random. You will get a different set of numbers and result each time. You can play with it as long as you want, and as often as you like.

The result of each equation is hidden when the page is initially loaded. You can click the “Display Answer” link to reveal the answer. Clicking the “New Question” link will generate a different set of random numbers. Navigation is located at the top. You can select: Addition, Subtraction, Multiplication and Division. Give it a try.

Just a couple of other things that I wanted to mention. I’m using Javascript to hide the answers when the documents are initially loaded. The math result is contained inside a hidden div. The div is toggled on or off or displayed or hidden, by simply clicking on a link. The Javascript that I use can be viewed and copied if you look at the source.

In addition, I’m using simple a rand() function to generate a set of numbers. I use basic PHP math operations to perform the math operations. I do a couple of checks in subtraction and division scripts to avoid the following: subtraction which results in negative number, division by zero, and a divisible number, meaning no remainders when performing divisions. The scripts can be easily modified.



$a = rand(0, 20);  // This generates a random number between 0 and 20.
$b = rand(0, 20);  // This generates a random number between 0 and 20.
$c = $a + $b; // Performs addition


$a = rand(0, 20);  // This generates a random number between 0 and 20.
$b = rand(0, 20);  // This generates a random number between 0 and 20.
if ($a>$b) {$c=$a-$b;} else {$c=$b-$a;}  // No negative results


$a = rand(2, 100);  // This generates a random number between 2 and 100.
$b = rand(2, 100);  // This generates a random number between 2 and 100.
do {
$c = $a / $b;
} while ($a % $b != 0) // Checks if result is divisible

It was a fun little project.

Differences Between CodeIgniter 1.7.2 and 2.0.0

It has been a year since I last touched CodeIgniter. At that time, I was working with version 1.7.2. Wanting to get back into it, I revisited the CodeIgniter website last week, and I learned that there is a new version of CodeIgniter, version 2.0.0. Last year, I created three personal projects that helped me learn how CodeIgniter works, as well as learn how MVC frameworks work in general.

I downloaded the latest version of CodeIgniter hoping that I should be able to move my old projects into the new version of CodeIgniter. Needless to say, the move was unsuccessful. I found out there are some significant changes to how CodeIgniter controllers and models are constructed between version 1.7.2 and 2.0.0. I should mention also, that there was a version in between that was released, version 1.7.3.

Here are the major differences in the way controllers and models are constructed between version 1.7.2 and version 2.0.0. In both cases, I’m using class constructors.

Version 1.7.2


class Contact extends Controller {
  function Contact() {


class Contact_model extends Model {
  function Contact_model() {

Version 2.0.0


class Contact extends CI_Controller {
  function __construct() {


class Contact_model extends CI_Model {
  function __construct() {

I might add, there are other changes I haven’t even mentioned. The biggest change is the removal of PHP 4 support. Version 2.0.0 requires PHP 5.1.6. Scaffolding is deprecated. Plugins have been removed in favor of Helpers, and few more.

Replace eregi with preg_match

A couple of weeks ago, I wrote a small PHP code to validate if the url being passed is in fact valid. I used a regular expression and PHP’s eregi function to find a pattern match. Looking back, the problem is, the eregi function is deprecated in PHP 5.3.0.

For compatibility going forward, we need to replace eregi with the preg_match function which is better suited for regular expression matches. We will use the same example as last time. As you recall, the function was:

Code With eregi

$urlregex = "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";
if (eregi($urlregex, $_POST['url'])) {
//  url is valid. shorten long url
} else {
// reject. url is invalid

Code with preg_match

$urlregex = "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";
if(!preg_match($urlregex, $_POST['url']))
// reject. url is invalid //
} else {
url is valid. shorten long url

Here, we’ve sucessfully replaced the eregi function with preg_match.

Form Validation Callbacks Within CodeIgniter

There are many ways to add form validation within CodeIgniter. One way is by extending the form validation library with your own. Another way is by using callbacks. CodeIgniter support callbacks to form validation. I would like to show how callbacks can be added to the existing form validation set of rules.

Validation Rules

Here’s a typical form validation rule where the ‘url’ field is required.

$this->form_validation->set_rules(‘url’, ‘URL’,'required’ );

Validation Rules with Callbacks

This is how you add callbacks. Notice the ‘callback’ prefix.

$this->form_validation->set_rules(‘username’, ‘Username’, ‘callback_url_check’);

The Callback Function

The callback function. The ‘callback’ prefix is omitted.

function url_check() { 
  $regex = “your reg ex code here”;
  if(!preg_match($regex, $str)) {
    set_message(‘valid_url’, ‘Please enter a valid URL.);   return FALSE;
  } else {
    return TRUE;

In the example above, you will need to supply your own regular expression to perform a url match. This is how you implement the callback function. It’s ideal for adding quick functions without extending the current CodeIgniter library.

Make Your Copyright Notice Dynamic

Most website and blog owners place some kind of copyright information on their websites and in their blogs. Copyright notices mean that the copyright holder, reserves all the rights provided by copyright law, such as distribution, performance, and creation of derivative works.

Copyrights are usually found in the footer of most designs, themes or templates. My copyright information is found in the footer. It says, “Copyright © 2003-2011. All rights reserved.” The copyright notice will need to be updated yearly to reflect the current year.

Some owners place a range of years, such as 2000-2010, just like mine, to reflect that the website or blog has been around for a number of years. Often times, the owner forgets to update the copyright information. It’s a simple text change, but there are ways to make it dynamic.

By making your copyright notice dynamic means you never have to edit the copyright notice ever again. At every New Year’s eve, while you are out celebrating, your website magically updates the copyright notice to the latest year. How? It uses a PHP function called date().

To get the current year, use the PHP date function:

<? echo date(“Y”); ?>

If you want a range of years, you can use:

2001-<? echo date(“Y”); ?>

There you have it, a dynamic copyright notice! And one more thing, to display the copyright symbol properly, use the HTML special symbol:

&copy; or &#169; which are the same as the symbol ©

Going Back To CodeIgniter

It’s has been months since I played around with CodeIgniter. I say, it’s time to head back into it. Or should I say, jump back head first. I just realized how much easier it is to whip out an application or two, by just piecing together a few lines of code.

The resulting code is light, fast and fairly efficient. I tried other PHP frameworks, like CakePHP, but for some reason I can’t really explain, I just couldn’t latch on to it. It’s a weak try on my part, but I just feel much more comfortable with CodeIgniter. Maybe, it’s the simplicity.

In any case, both CakePHP and CodeIgniter requires some heat, starting a fire, and some baking. But, first off, I need to get reacquainted with CodeIgniter.

I’m back reading The User Guide.

PHP Time of Day Script

One very cool way of taking advantage of PHP’s powerful language is to make a determination as to what time of the day it is, and making a decision based on that information. For example, we want to detect the server’s current time and display a message that says either good morning, good afternoon or good evening.

There’s a handy PHP function called date that we can use. We can assign a variable called $current_time, to the server’s current time.

$current_time = date(G);

Next, let’s display the logic.

if ($current_time >= 0 && $current_time < 12) { echo 'Good Morning';}
if ($current_time >= 12 && $current_time < 18) { echo 'Good Afternoon';}
if ($current_time >= 18 && $current_time < 24) { echo 'Good Evening';}

You can also display images based on time of day.

if ($current_time >= 18 && $current_time < 24) { echo<img src=”sample.jpg” />;}

Display Your WAN IP Address

Have you ever wondered how dozens of websites can display your WAN IP address? The script is quite simple. You can probably host it if you have access to a web server. All you need to do is create a PHP file. Place the code below. Save. Upload the file to a web server and run the script. That’s it.

$ip = getenv(REMOTE_ADDR);
echo “Your IP address is:. $ip;

My WordPress Multi-Site .htaccess File

I just solved my WordPress multi-site problem. I have several blogs located in the subdirectories of this blog. The problem was the media files for my sub-blogs were all broken. If you are aware of the WordPress multi-site setup, WordPress creates a blog.dir directory under wp-content. This is where it keeps all of the uploaded media files such as: images, video, etc for the sub-blogs.

The issue was began when my .htaccess file was overwritten or possibly replaced. At first, I thought it was just an issue with the WordPress options pages which is stored in the wp-options table. But, that wasn’t the case. A quick search in the WordPress forums solved that problem. So, I’m writing this article for two reasons: (1.) So others can benefit, (2.) so I have a record of this fix somewhere.

So, here’s my .htaccess file.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
# uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-includes/ms-files.php?file=$2 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ – [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress

Details for WordPress 3.1

The WordPress development team met last week and laid out the plans for WordPress 3.1 scheduled for release on December 15. Here are the details:

The finalized goals for WordPress 3.1 are “to have a very short dev cycle, a decent amount of testing time, and a release in mid-December. Low on new features, heavy on ui and code cleanup, and avoidance of schema changes. Save the big ideas for 3.2 where we’ll have the liberty to implement those ideas in PHP5. No schema changes and no big new APIs.”

Besides bug fixes and code cleanup, users can look forward to a new inspired admin bar and theme browser, as well as post templates and styles, and a separate network dashboard.

The team plans to halt new feature submissions on October 15th, followed by a primary code freeze on November 1st, and a string freeze on December 1st. The beta period will begin November 15th until the estimated final release date of December 15th.

There is not a lot of cosmetic changes, but I’m looking forward to the new Admin pages that’s similar to