PHP Array Mode Function

7th January 2008 - 2 minutes read time

The following mode function will return the most commonly occurring value from an array of values, also called the mode. If just the array is used then only the most commonly occurring value will be returned. The second parameter can be used to return an array containing the mode and the number of times that this value occurs in the array.

Rounding A Number To Nearest The Thousand In PHP

6th January 2008 - 3 minutes read time

I have previously talked about rounding numbers in PHP, but what if you wanted to round the number to the nearest thousand?

It is possible to do this with the native round() function in PHP by using a negative number as the second parameter. The round() function has two parameters, the first is the number to be rounded and the second is the number of places to round the number to, also known as the precision. The default for round() is to round the number to the nearest whole number, by using positive numbers as the second parameter you can set the number of decimal places to round the number to. Giving a negative number as the second parameter will allow you to round the number to the nearest full number. For example, to round a number to the nearest thousand you can put -3 as the precision, this will change the number 12,345 into 12000.

The PHP User On Linux

5th January 2008 - 2 minutes read time

Reading or writing a file using PHP is quite a common practice, but it can often fall cause programs to fall flat on their face if the proper user privileges are not in place. Although this is not a problem on Windows machines due to the lack of a proper security model, but on Linux machines you need to make sure your scripts can run with the correct permissions.

First you must determine what the name of the user and group is. On OS X the default is "www" for both user and group. If you are using Apache then the user and group information is kept in the http.conf file. Look for a couple of lines that look like this, this is the default settings for Apache 2.2.

Postponing Code Running In JavaScript

4th January 2008 - 4 minutes read time

Creating user interfaces in JavaScript can sometimes lead to a problem, especially when the interface used AJAX to load data from the server. As many actions will be event driven by the user you can find that when a user triggers lots of events all at once the browser will send out lots of AJAX requests all at once. This can easily cause bandwidth issues but can also lead to the user getting confused while they patiently await the browser to settle down and let them get on with things.

There are numerous way to get around this issue, the first being to design programs so that AJAX requests are only issued when the user has clicked on something or when they start typing

For Loop Debugging In JavaScript

3rd January 2008 - 5 minutes read time

The for loop in JavaScript can be used to iterate through all items in an array or properties of an object. This makes looping through any object or array very easy. As in the following example that printing out all items in an array.

script.aculo.us Confine Draggable

2nd January 2008 - 3 minutes read time

Creating a new Draggable object with the script.aculo.us framework is easy. You just create a new instance of the Draggable object with the element id to be made draggable as the first parameter and a set of options as the second.

Toggle a TINYINT Field in MySQL

31st December 2007 - 1 minute read time

MySQL uses the datatype TINYINT to store boolean values. MySQL stores the value as TINYINT(1) which is the same as a bit so the value is either 0 (false) or 1 (true). Using boolean fields can be very useful, but it can be costly in processing as to change the value you have to query the database, find out the value of the field and then act accordingly.

Here is a simple MySQL query that can be used to toggle the value already present in the TINYINT field without having to do any pre-querying.

UPDATE table SET field = 1 - field

Avoiding If Statement Typos In PHP

30th December 2007 - 2 minutes read time

It is very easy to break a program with a simple typo. Instead of typing == when comparing two values you type = and actually assign a variable. This is an easy way to introduce a bug as you will not always notice it until your program doesn't work. There is an easy way of avoiding this.

By using the following syntax:

if(100 == $score){ }

Instead of the usual format:

It is very easy to break a program with a simple typo. Instead of typing == when comparing two values you type = and actually assign a variable. This is an easy way to introduce a bug as you will not always notice it until your program doesn't work. There is an easy way of avoiding this.

Some Useful String Functions In JavaScript

27th December 2007 - 4 minutes read time

Here are a few of the built in JavaScript functions available.

To get the length of a string use the length variable. This returns the number of characters in a string.

  1. var str = 'abcdef';
  2. str.length; // returns 6

The charAt function will return the character at the point specified in the parameter. So to get the first character of a string use: