|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS CF101 Creating Variables in CFML
The basis of many CF programming projects
By: Jeffry Houser
Feb. 25, 2004 12:00 AM
Welcome to CF101, a new column I'll be writing for ColdFusion Developer's Journal. This column is dedicated to all of you beginners out there, to teach you the basics of ColdFusion development. You don't need to have a prior understanding of programming, HTML, or Web development to read this column, although if you do, it's a great way to help reinforce the basics. For the first column, I thought I'd talk a bit about what variables are and how we can create and use them in ColdFusion. Before we jump into that, I thought you might like to know a little bit about my development experience. Who is Jeffry Houser? Around 4:59 one Friday evening, I was handed a project with a Monday morning deadline. Another developer had left the company for greener pastures and one of his projects had been left unfinished. The project was being built in ColdFusion with Microsoft Access in the database. Although I had no previous ColdFusion training, I was able to complete the project in 10 hours on a rainy Saturday. I haven't looked backed since. I left that small advertising firm in 1999 to start DotComIt, a Web consulting company. Since starting DotComIt, I've written three ColdFusion books, including ColdFusion: A Beginner's Guide; spoken at a bunch of user groups; and written a handful of articles. I've dealt with a lot of technologies over the years, but ColdFusion remains my favorite Web development technology because of its simplicity. I've always had a knack for describing the complex in a straightforward way, and that is why the folks at CFDJ thought of me when they decided to create this column. I have also been a musician for over 20 years and own a recording studio. I mention that only because many people find it more interesting than my Web development adventures. Hopefully my life story doesn't stick in your head for too long; let's talk about variables. Understanding What a Variable Is You might want to ask why we would want to use a variable instead of just using the value that it points to. That would be a good question; and there are multiple reasons. First, a variable's value can change, but the variable name never does. If we use the variable in 100 spots, then we only have to change it once, not 100 times. This could be a real time-saver when writing code. For instance, if the Webmaster's e-mail address needs to be displayed in various places in a site, storing the address in a variable and then displaying the value of that variable would mean that if the Webmaster's e-mail address changed, the value of the variable would need to be changed only in the one place where it is set, as opposed to having to change every place where the address is used. A second reason for using variables is that you may not know what the value of the variable is going to be. One of the more common uses of this is when you are accepting input from a user, usually through an HTML form. Variables can be used to verify the user input, insert it into a database, mail it to someone, or perform whatever other processing you need to do. Creating Variables with <cfset> <cfset variableName = value> As with all ColdFusion tags, it starts with the name of the tag, <cfset>. After that you specify the name of the variable that you want to create. Then comes the equal sign, followed by the value you want to give the variable. ColdFusion has some special conventions that you must follow when chosing a variable name. If you do not follow these rules, ColdFusion will display a nasty error message when you try to execute a template. These are the rules:
A variable can hold many different types of values. They can take simple values such as integers, real numbers, strings, or Booleans. An integer is a whole number, such as 12 or 205. A real number is a number with a decimal, such as 12.5 or 19.3. Strings are text values such as "This is a cfset example." Strings are always placed inside quotes, while numeric values are not. A Boolean value has one of two possible values, true or false. In CFML, the words "true" and "false" and "yes" and "no" are Boolean values. A number can also be used as a Boolean value - zero is false and all other values are true. Complex values such as arrays, structures, or ColdFusion Components can also be assigned to variables with the <cfset> tag. We'll reserve the discussion of complex values for a future column. An expression can also be contained in the value portion of the <cfset> tag. ColdFusion will execute the expression and use the result as the value of the variable. We'll reserve an in-depth discussion of ColdFusion expressions for another article. Right now you can refer to the Macromedia documentation to learn more: http://livedocs.macromedia.com/coldfusion/6.1/ htmldocs/cfml_b14.htm#wp1160241. Creating Some Variables <cfset FirstName = "Jeffry"> The first line creates a variable called FirstName with a string value of Jeffry. The second variable creates a variable called LastName with a string value of Houser. The third creates a variable called Age with an integer value of 28. Then we create a variable called State and give it the value of "California". The next line changes the value of the state variable from "California" to "Connecticut". The final line of the code segment changes the state variable's value from a string to an integer. This is an example of changing a variable's type - something that's very easy to do in ColdFusion. This is different from the way many other programming languages operate, and is part of the reason why ColdFusion is very easy to work with. You can easily change a value from an integer to a string and back just by placing another <cfset> tag. Outputting (Displaying) Variables <cfoutput> Before going too much further, I want to specify that this code segment must be put in the same template as the previous code segment to execute properly. This listing starts with the <cfoutput> tag. When the ColdFusion server sees the <cfoutput> tag, it knows to process all the text between the beginning <cfoutput> and the end </cfoutput>. The difference between the start and end tag is that the end tag has a slash before the tag name. This is identical to the way that HTML handles start and end tags. Not all tags have both a start and end tag. The <cfset>, for instance, has no closing tag. You can place any sort of text that you want in the <cfoutput> tag block. The way to tell ColdFusion the difference between normal text and an expression that is to be evaluated is with the use of the pound sign, "#". Text between two separate pound signs is evaluated as an expression. Make sure that there are no spaces between the pound signs and the expression. Any text without pound signs is completely ignored by the ColdFusion server and is passed on to the browser unmodified. This text could be HTML, JavaScript, CSS, or any plain text. The second line of the template contains a ColdFusion expression and an HTML tag. ColdFusion will look at the expression and return the value of the FirstName variable, "Jeffry". The HTML <br> tag will go straight to the browser. The same happens for the LastName, Age, and State. The code segment finishes with the end cfoutput tag. The output should look like this: Jeffry You'll notice that the State variable contains the most recent value it was set to, and the other values, "California" and "Connecticut", are lost. Variable Scopes ScopeName.VariableName Just as you could mix case sensitivity with variable names, you can also do so with variable scopes. They are not case sensitive. Here are some common variable scopes:
In our code example from the previous section, we did not specify a variable scope. When no scope is specified, the variables are placed in the local scope, which is named "variables". We could easily rewrite the previous listing to look like this: <cfoutput> This code segment works identically to the one that did not specify the variables' scope before the variable name. Conclusion YOUR FEEDBACK
CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||