PHP Logo

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.

By using the following syntax:

if(100 == $score){ }

Instead of the usual format:

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.

var str = 'abcdef';
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:

var str = 'abcdef';
str.charAt(0); // returns a

To get the last character of a string use a combination of charAt and length.

var str = 'abcdef';
str.charAt(str.length - 1); // returns 'f'

To get the position of a character or group of characters in a string use the indexOf function. The function returns -1 if the string is not found.

HTML Logo

External JavaScript Include In HTML

26th December 2007 - 1 minute read time

To include a JavaScript file into a HTML page you can use the script tag with the src parameter pointing towards the source code file. However, there is a subtle difference between the script tag in HTML and XHTML. This is because the language attribute is not supported in XHTML, so if you just copy the code from HTML to XHTML the page won't validate. The solution here is to just leave it out.

For HTML

<script language="JavaScript" type="text/javascript" src="scripts/javascript.js"></script>

For XHTML

<script type="text/javascript" src="scripts/javascript.js"></script>

The type attribute is the mime type of the script, which is always text/javascript. Although there is come discussion about what the mime type is supposed to be, best results are usually obtained by just sticking with text/javascript.

MySQL Logo

Getting A Random Row From A MySQL Table

26th December 2007 - 1 minute read time

Getting a random row from a MySQL table requires the use of the RAND() function in the ORDER BY clause of the SELECT statement. This will generate a new random number for each row and order them by that new number. In order to get a single row this is combined with the LIMIT clause to limit the result to a single row.

SELECT * FROM theTable ORDER BY RAND() LIMIT 1;
PHP Logo

Odd and Even Numbers in PHP

25th December 2007 - 2 minutes read time

To find if a number is odd or even you can use one of two operators.

The modulo operator (% in PHP) can be used to calculate the remainder of the value divided by 2. This gives a value of 0 for even numbers and a value of 1 for odd numbers. This can be used in an if statement as 0 will equate to false and 1 will equate to true.

$value = 10;
if ($value % 2) {
  echo '$value is odd';
} else {
  echo '$value is even';
}

The second method is to use the & (AND) operator with the number 1. This will perform a bitwise calculation on the number and 1, returning 0 if the number is even and 1 if the number is false. So using the same if statement logic as before we can write.

#! code Logo

JavaScript Function isNaN

24th December 2007 - 1 minute read time
Small JavaScript function to check if a variable is a number.
PHP Logo

Rounding And Displaying Numbers In PHP

24th December 2007 - 3 minutes read time

To round a number in PHP you can use one of three functions, these are round(), ceil() and floor(). All of these functions take number as the input and will round the value depending on the function used.

To round to the closest integer use the round() function.

round(4.4);  // returns 4

To round down to the nearest whole number use the floor() function.

floor(4.4);  // returns 4

To round up to the nearest whole number use the ceil() function.

PHP Logo

Does A String In PHP Contain A Number?

24th December 2007 - 2 minutes read time

The is_numeric() function in PHP can be used to see if a number contained in a string is numeric. The function returns true is the variable can be parsed into a string, otherwise it returns false. Here are some examples:

is_numeric('five')  // returns false
is_numeric(123); // returns true
is_numeric('123'); //  returns true
is_numeric(-123); //  returns true
is_numeric('-123'); //  returns true
is_numeric('123.4'); //  returns true
is_numeric('1,234'); //  returns false

Notice that if your number has a thousand separator in it the function will return false. In this case you need to use the str_replace() function to strip out the commas before passing the value into is_numeric(0).

PHP Logo

Generate A Select Box For A HTML Form

21st December 2007 - 1 minute read time

Here is a function to generate a select box for a HTML form. The three parameters are:

  1. $name : The name of the select, this will appear in the "name" attribute.
  2. $options : An associative array containing all of the options.
  3. $default : The option that will be selected as default.
function generateSelect($name,$options,$default=''){
  $html = '<select name="'.$name.'">';
  foreach ($options as $value => $label) {
    $html .= '<option value="' . $value . '">' . $label . '</option>';
  }
  $html .= '</select>';
  return $html;
}

You can call the function like this:

echo generateSelect('selectPreference',array('yes'=>'yes','no'=>'no'),'yes');

Which produces the following HTML as output:

PHP Logo

Printing Arrays Using print_r()

20th December 2007 - 2 minutes read time

When debugging PHP code the print_r can be useful if you want to know what an array or object contains. It will take any variable as input and will print off as much information as it can about that variable. The following code.

$array = array(1,34,6,2325,5,34,2);
echo "<pre>";
print_r($array);
echo "</pre>";

Will produce the following result.

Array
(
[0] => 1
[1] => 34
[2] => 6
[3] => 2325
[4] => 5
[5] => 34
[6] => 2
)

Notice the use of the pre tags. This is to allow the output to be properly formatted on screen, otherwise it looks messy. Of course you could just view source, but I find the formatted output easier on the eye. You could also write it out like this: