PHP Logo

Debug Your PHP Applications With Krumo

20th April 2008 - 3 minutes read time

Krumo is an open source plugin for your programs that is designed as a replacement to print_r() and var_dump(). These functions are used by developers (myself included) to find out what the program is doing. The main problem is that if there is a lot of data to look at the page can get a bit busy.


Krumo solves this by simplifying the output into a more readable format. It tells you the format of the array or object item and any other information that it can gain. It also puts the data into a set of clickable sections so that if you are interested in a particular section of output then you can click on it and see only that section.

This tool only has three files, the PHP code to integrate it into your projects, the JavaScript to create the clickable elements and the CSS to give the output some style. It is definitely worth a look.

PHP Logo

Cut A String To A Specified Length With PHP

17th April 2008 - 4 minutes read time

Cutting a string to a specified length is accomplished with the substr() function. For example, the following string variable, which we will cut to a maximum of 30 characters.

$string = 'This string is too long and will be cut short.';

The substr() function has three parameters. The first is the string, the second is where to start cutting from and the third is the amount of characters to cut to. So to cut to 30 characters we would use the following.

$string = substr($string,0,30);

The string variable is now set to the following.

This string is too long and wi

This example highlights the major problem with this function in that it will take no notice of the words in a string. The solution is to create a new function that will avoid cutting words apart when cutting a string by a number of characters.

PHP Logo

Get Functions And Variables Of An Object With PHP

16th April 2008 - 3 minutes read time

It is possible to find out what functions and variables are available from an object at runtime using the PHP functions get_class_methods() and get_object_vars().

Take the following class called testClass.

class testClass {
 public $publicVariable = 'value1';
 private $privateVariable = 'value2';
 public function testClass()
 public function aPublicFunction()
 private function aPrivateFunction()

To find out the functions available from the class you can use the function get_class_methods(). This takes either a class name as a string or an instance of the object. The following bit of code will print out all of the functions in the class.

PHP Logo

Getting The Current URI In PHP

15th April 2008 - 2 minutes read time

The $_SERVER superglobal array contains lots of information about the current page location. You can print this off in full using the following line of code.

echo '<pre>'.print_r($_SERVER, true).'</pre>';

Although this array doesn't have the full URI we can piece together the current URI using bits of the $_SERVER array. The following function does this and returns a full URI.

function currentUri(){
 $uri = 'http';
  if($_SERVER['HTTPS'] == 'on'){
   $uri .= 's';
 $uri .= '://';
 if($_SERVER['SERVER_PORT'] != '80'){
 return $uri;

You can use this function like this:

PHP Logo

The Final Keyword In PHP5

14th April 2008 - 3 minutes read time

PHP5 allows you to stop classes being extended or to stop child classes overwriting functions.

The first way to use the final keyword is to stop child classes from overwriting functions when they are created. This can be used to stop an important function from being overwritten. To use the final keyword here just add it to the start of function name.

class ParentClass{
 final public function importantFunction() {
  echo 'ParentClass::importantFunction()';
class ChildClass extends ParentClass{
 public function importantFunction() {
  echo 'ChildClass::importantFunction()';
$child = new ChildClass();

Attempting to override this function will produce the following error.

MySQL Logo

Get MySQL Version Information Through PHP

13th April 2008 - 2 minutes read time

There is little syntactical difference between MySQL 4 and MySQL 5, but sometimes finding that difference can pinpoint a bug. The mysql_get_server_info() function will tell you what version of MySQL you are using. You can call it with no parameters, in which case it picks the most recently created MySQL resource, or with the resource handle created with mysql_connect().

Here is an example of how to use it.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
echo mysql_get_server_info();

You can achieve the same effect with a simple MySQL query.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$query = mysql_query('SELECT VERSION() as mysql_version');
PHP Logo

Using Multiple Arguments To A Function With parse_str() In PHP

12th April 2008 - 3 minutes read time

Sending multiple arguments to a function can be done using a parameter string. This is just like a URL that has data encoded into it. For example, if you wanted to send two parameters (called parameter1 and parameter2) to a function then you would use the following string.


To use this in the function you create the function as normal with a single parameter. This single parameter is the string that will contain all of your arguments.

function test($arguments)

You must run the parse_str() function on the arguments parameter to extract the data you need. You can then call the parameters by their names as variables.

Linux Logo

View Directory List After Entering

11th April 2008 - 2 minutes read time

When navigating the file structure in Unix/Linux environments you will often find yourself typing cd to change the directory and then immediately typing ls to see the contents of the directory.

It is possible to run ls automatically every time you run cd by adding the following commands to your .bashrc file.

cd() {
 if [ -n "$1" ]; then
  builtin cd "[email protected]" && ls
  builtin cd ~ && ls


Linux Logo

Simple Trick To Run Last Command As Sudo

10th April 2008 - 2 minutes read time

You can often forget what you are not running as a super user, so if you type in a command that you can't run with your current set of privileges it will tell give you a permission denied response.

An alternative is to use the !! command to run the last command in the .bash_history. Use this with the sudo command to run the last command as a super user.

> command
Permission denied
> sudo !!
PHP Logo

Faster Way Of Checking String Length With PHP

9th April 2008 - 2 minutes read time

If you want to know the length of a string in PHP you would normally turn to the strlen() function which simply tells you the length of the string.

$string = 'this is a string';
$strLength = strlen($string);

To use this to check the length of the string use the following example.

$string = 'this is a string';
if (strlen($string) < 20) {
 // code here

A quicker way of looking at the length of a string would be to use the isset() function in conjunction with the curly braces {} used for locating character from a string.

$string = 'this is a string';
if (!isset($string{20})) {
 // string too short

Because isset() is a language construct it works quicker than strlen() so this comparison has almost no overhead at all.