Examining Functions

I l @ ve RuBoard

Functions are another integral part of ColdFusion. They give you the ability to perform different types of tasks . There are functions to work with many types of variables, such as dates, times, and text variables. There are also functions for formatting text variables . Each function performs a specific task. Some functions create dates, or add to dates. Some functions remove white space from variables. Other functions perform mathematical operations such as taking the absolute value for a number, determining the sine or cosine of a number, or even generating random numbers . There are numerous functions to work with arrays, lists, and structures, which are more advanced variables.

Boolean functions return either a true or false value. Boolean functions can determine if a variable exists, or if a variable is a valid date, structure, or array.

Some functions are passed parametersthat is, a value is passed to the function so that the function can work with that value. For example, when you create today's date you use the Now() function, but in order to display that date in a manner that doesn't look like a mess of numbers, you use the Dateformat() function. In order to work, the Dateformat() function must be passed a date to format as well as a mask, which tells the function how you want the date formatted. Passed parameters are placed between parentheses. If a function is not passed any parameters, then the parentheses are left empty, as with the Now() function.

You can even create your own functions, if one doesn't already exist to perform a task.

Using functions is a simple process. In the previous task you created a variable that held today's date. However, when it was output, it didn't look the way we usually expect dates to look. By using Dateformat(), you are able to control how that date is displayed by specifying different masks.

  1. Make sure you're still in date.cfm.

    This is the file you created in the task above.

    If for some reason you don't have the file, it is available on the CD in the folder Lesson3\Complete\date.cfm. However, the copy of the file on the CD is already complete; if you do not wish to use the completed file, you will need to recreate the file by following the steps in the first task again.

  2. Change the #Today# text in the body to #Dateformat(Today, "mm/dd/yy")#.

    Your code should now read:

     <cfoutput>#Dateformat(Today, "mm/dd/yy")# </cfoutput> 

    You're using the Dateformat function in ColdFusion. This function takes the date passed to it and formats it however you specify; in this case that will be mm/dd/yy. Yes, that's as simple as it seems. In this exercise, you're passing the Dateformat the Today variable, which currently holds the value {ts '2001-10-15 18:49:54'} . Now, when the variable is output, it should say 10/15/01. The variable itself will still hold the value {ts '2001-10-15 18:49:54'} because the function was run on the variable as it was being output, and doesn't actually affect the original variable. If you wanted the variable to be permanently switched to that format, you could have reset the variable using <cfset Today = Dateformat(Today, "mm/dd/yy")> . Then, the variable would hold the value 10/15/01 and would display 10/15/01 whenever you output it. Of course you could change the format when you output the variable using Dateformat() again.

    TIP

    A quick way to add a function is to right-click and select Insert Expression from the contextual menu that appears. This opens the Expression Builder, which contains a complete list of ColdFusion functions. Just click on a function and press the Insert Expression button to insert the function into your document.

  3. Click File > Save or press Ctrl+S to save the document.

    Always remember to save your ColdFusion files any time that you make a change.

  4. Open a browser and type http://127.0.0.1/Lesson3/Start/date.cfm into the address window, and then press Enter.

    Instead of a page where the date reads "{ts '2001-10-15 18:49:54'}," ColdFusion has now produced a page that says "10/15/01." If you view the HTML source code once the browser has finished loading the page, it looks identical to that from the task above, except that the date is now formatted.

    Don't close date.cfm yet.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net