Main Page
Web Site Design Services
  Search Engine Optimization
  Flash Web Sites
  Coldfusion Web Sites
Our Web Sites
  View Our Portfolio
  Law Firm Web Sites
  Real Estate Web Sites
For Developers
  Developers Center
  Flash Resources
  JavaScript Resources
  PHP Resources
Contact Us
Name
Email
Phone
Comments

Developers :: PHP Resources :: Formatting Output Using PHP

By Tom Fitzgerald

Ok we learned variables, now what?

Now we can learn how to change the output of certain variables. For example if you have variable containing the number 3, what if you want to output it as $3.00, or $0.03 ? How can we do that easily? There are some other cool things we can do but as usual; lets actually view the code and then explain how to do it. There are two commands we start to use here, printf() and sprintf().

printf("format",$firstvariable, $secondvariable);
$variable = sprintf("format", $variable1, variable2);

printf() outputs formatted strings and the sprintf() does the same thing but instead outputs into a variable to be stored and used for later.

$variable1 = 'Tom';
printf("% says hello",$variable1);

Here we have the printf() function you will notice the % tells PHP to output the variable supplied as the second argument to the function. We could stop there but printf() has so many more neat functions, let see. What other things we can add to that lonely %. The syntax is as follows:

%padding-width.decimaltype

  • The padding is a padding character that we can use to fill in the blanks so to speak with the value we use is smaller than the formatting width specified. (We haven't done that yet). If you don't specify a padding character, no big deal; a space is used.
  • The - symbol makes the text left justified, if this is not present it will be right justified.
  • The width is the number of characters to pad using the padding character.
  • The decimal is the decimal places to use.
  • The type is the type of value, the command values are s for string or f for float (or numbers with decimal places)

For example if the padding character is 0 and the with is 3 and the value is 5 then output would be.

001

Ok enough of all that, let's see it in action!

$paycheck = 250;
$newvariable = sprintf("My paycheck is $%03.2f");
printf($newvariable);

The output would be :

$250.00

The $%03.2f is interpreted as follows; the % says to start outputting the variable. The 0 says to pad using variables. The 3 says to put 3 places to the left of the decimal point and the 2 says to go 2 places to the right of the decimal point.

Other Output Functions

Let's take a look at some other output functions, these are really quick simple examples, starting with Strtoupper().

strtoupper() - Converting to upper case

$newvariable = strtoupper($variable);
printf($newvariable);

Takes the $variable and outputs in all uppercase. You can use unfirst() to capitalize only the first letter in the string to uppercase, likewise ucwords() will upper case each word in the string to uppercase.

strtr() - Replacing values

$variable1 = "1 6 3";
$newvariable = strtr($variable1, "6","2");
printf($newvariable);

This takes $variable1 and reads the string, replacing 6 with 2, then outputs 1 2 3

substr() - Returns value inside of string at a specific point

$newvariable = substr("tommy", 2,4);

Reads "hello" and returns everything from the 2nd character to the 4th character. Outputting:

mmy

substr_count() - Counts number of times a given item is in a variable

$variable1 = "tom 123 tom 123 tom 123";
$find = "tom";
$numberoftimesfound = substr_count($variable1, $find);

In this case the variable $numberoftimesfound would have a value of 3 because it found tom 3 times in $variable1.

Using Date Format

Ok now we did some cool stuff, let's see how simple it is to use the date since that is used very heavily in most PHP applications. Like almost everything else in PHP, accessing the current date is very easy, let's take a look.

$today = date("d/m/y");

Would give the $today variable a value of 04/01/2006. Very easy! :)

All Information Copyright 2006 Certified Computer Experts, Inc.