PHP == or ===

Many PHP developers get confused when using the comparison operators.  Mainly weather to us two equal signs or three.  It really is a simple concept when you understand what it does.  The double equal sign (==) compares two data types which don’t have to be in the same type.  So for example you can compare

$a = 1

$b = ’1′

$a is a integer and $b is a string. That would return true under a double equal sign.

However, the triple equal sign (===) the data types must be the same on both sides of the operand.  So… the above would return false since $a is a integer and $b is a string.

So just remember that double doesn’t matter on data types triple must be the same data type.

Backtick Operator

I just learned something new today in PHP.  It’s the Backtick Operator.  What it does is executes a shell command and captures its output.  You can then store that in a variable for use.  I am pretty sure this is a UNIX command only.  Not sure if this will work in windows or not.  Here is how you use it.

< ?php
$a = `ls -l`;
echo $a;
?>

Date Validation with CI

The new release of CodeIgniter (1.6.2) still doesn’t have a date validation function included in the validation class. However, no need to worry. I will show you in a couple easy steps how to add this in.

First off let’s start by saying that we will validate dates in the MM/DD/YYYY format. If you need to change this it only takes a couple of simple edits to the regular expression.

The files that need to be edited are the validation.php (located @ CI → System → Libraries) and the English Language Validation file, validation_lang.php (located @ CI → System → Language → English).

Here is the code to check to make sure the date is in the right format and contains numerics.

/**
* Valid Date
*
* @access    public
* @param    string
* @return    bool
*/
function valid_date($str) {
if(!preg_match("/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/",$str)){
return FALSE;
}
else {
/* 1. Extract the numeric data presented as MM/DD/YYYY */
list($MM, $DD, $YYYY) = explode("/",$str);
/* 2. Validate the date */
if(!checkdate($MM, $DD, $YYYY)) {
return FALSE;
}
}
}

// --------------------------------------------------------------------

You can add this code to the end of the validation.php file.

Now in your controller when you are validating data you just have to add valid_date text to the mix. However, you still need to add an error message into the validation dictionary. You can add anything you want but open up the validation_lang.php and append the following line of code.

$lang['valid_date'] = "The %s field must contain a vaild Date in the mm/dd/yyyy format.";

You can get more information on the CI validation class here .

That’s it..happy validating!

CodeIgniter Logging Library

CodeIgniter (CI) has a very good logging class but there is one thing in the class that I am not to thrilled with, The way it writes the logs. CI logging has 5 levels;

0 = Disables logging, Error logging TURNED OFF
1 = Error Messages (including PHP errors)
2 = Debug Messages
3 = Informational Messages
4 = All Messages

In the configuration file if you set the level at 3 you will get info messages (messages in the logs that you create) and everything that is less than 3. So you will get info, debug, and error messages. This will fill up your log files quite rapidly. On a project I am working on now one of the requirements is to log usage in log files. So I had to modify the logging class to log only Info messages and not everything less than what you set the threshold to. This was quite easy. I am not sure how many people want to do this but here is how you would go about modifying it in your code.

The file to edit is located in CI -> system -> libraries -> Log.php

On line 87 or around line 87 (give or take a line or two) you should find this line of code

if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))

Replace it with

if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] != $this->_threshold))

That’s It! You should now only get the Info Messages or what ever your threshold is set to in your config file. That configuration file is located in CI -> system -> application -> config -> config.php

Look for the section with the heading of “Error Logging Threshold”

As always if you have any question leave a comment.