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:

HTML Logo

Linking An RSS Or Atom Feed to a HTML Document

20th December 2007 - 1 minute read time

Adding a hyper link for an RSS or Atom feed on your web page works, but that's not all you can do. By adding a link to the head section of the page you can allow your users an alternative method of picking up your feed.

To add an RSS feed use this.

<link rel="alternate" type="application/rss+xml" href="http://www.example.com/rssfeed.xml" />

To add an Atom feed use this.

<link rel="alternate" type="application/atom+xml" href="http://www.example.com/blog/wp-atom.php" />

These are both XHTML examples. To do this in HTML just remove the slash on the right hand side like this.