Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ColdFusion 9 Developer Tutorial

You're reading from   ColdFusion 9 Developer Tutorial Create robust professional web applications with ColdFusion

Arrow left icon
Product type Paperback
Published in Jul 2010
Publisher Packt
ISBN-13 9781849690249
Length 388 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
John Farrar John Farrar
Author Profile Icon John Farrar
John Farrar
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

ColdFusion 9 Developer Tutorial
Credits
About the Author
About the Reviewers
1. Preface
1. Web Pages—Static to Dynamic 2. Basic CFCs and Database Interaction FREE CHAPTER 3. Power CFCs and Web Forms 4. ORM Database Interaction 5. Application, Session, and Request Scope 6. Authentication and Permissions 7. CFScript 8. CF AJAX User Interface 9. CF AJAX Forms 10. CF AJAX Programming 11. Introduction to Custom Tags 12. ColdFusion Powered Views 13. Control Logic Processing 14. Guide to Unit Testing Beyond this Book Tools and Resources Index

Understanding and using simple variables


In this section, we will look at a couple of variable types which will help you understand how ColdFusion works. First we are going to look at the different types of simple variables.

Variable types

There are four types of simple variables. These are string, numeric, Boolean, and date/time variables. They can basically have any name for a variable, but there are a few basic guidelines for naming variables:

  • A variable name must begin with a letter, which can be followed by any number of letters, numbers, and underscore characters. (We have found that you can start variable names with underscore also and there is enough code out there that it is unlikely to see this depreciated, if you use it.)

  • A variable name cannot contain spaces.

  • Variable names are not case-sensitive in ColdFusion. (myVariable is the same to ColdFusion as Myvariable.)

Integers

We will be covering two number classes in this lesson. First we will take a look at integers. These are numbers with no decimal value. ColdFusion supports integers between -2,147,483,648 and 2,147,483,647. It will work with numbers outside this range, but the precision will not be exact.

In the first example, we will modify the code we have been using to keep one of the string variables and add a numeric variable called myAge. The following two examples of code run identical to each other. It is fine to code via tag or scripted code:

Tip

Remember to run the code examples as you go. It enhances understanding and retention.

<!--- Processing --->
<cfset myQuestion = "This is my question.">
<cfset myAge = 27>

<!--- Content --->

<!--- Content --->
<cfoutput>
  myQuestion is (#myQuestion#)<br />
  myAge is (#myAge#)<br />
</cfoutput>

<!--- Processing --->
<cfscript>
  myQuestion = "This is my question.";.";
  myAge = 27;
</cfscript>

<!--- Content --->
<cfoutput>
  myQuestion is (#myQuestion#)<br />
  myAge is (#myAge#)<br />
</cfoutput>

Note

Note that when writing script, commands and assignments are completed by the use of a semicolon.

Strings

You will notice that strings are declared with quotation marks. These can be single or double quotation marks. Numbers do not use quotation marks. This would be the same in an expression, which is what you see in the preceding examples, as in a variable declaration. Expressions are what we call code when we combine strings, do math, or Boolean comparisons. Here we will look at our first expression example by changing both the string and the numeric variables in our code. ColdFusion 8 added the += operator to the platform. This is a widely used notation to say, add the right-hand value to the original value. (It also added &= for strings.) Run the following example:

<!--- Example: 1_3.cfm --->
<!--- Processing --->
<cfscript>
  myQuestion = "This is my question.";
  myAge = 27;

  myQuestion = myQuestion & " Is this a string?";
  myAge += 1;
</cfscript>

<!--- Content --->
<cfoutput>
  myQuestion is (#myQuestion#)<br />
  myAge is (#myAge#)<br />
</cfoutput>

The following screenshot shows the result after the previous code is run:

Note

There is no requirement to write things in CFScript like you will see in this book. Currently, script is used in JavaScript, AIR/Flash/Flex ActionScript, .Net, PHP, and JAVA. This is likely the most common form of coding.

Decimals

Now, we will look at decimal-based numbers. We will modify the code just a bit more to show more of the things you can do with numbers and remove the string functions for the time:

<!--- Example: 1_4.cfm --->
<!--- Processing --->
<cfscript>
  myAge = 27;

  halfAge = myAge/2;
</cfscript>

<!--- Content --->
<cfoutput>
  myAge is (#myAge#)<br />
  halfAge is (#halfAge#)<br />
  halfAge rounded is (#round(halfAge)#)<br />
  4.2 rounded is (#round(4.2)#)<br />
  4.2 ceiling is (#ceiling(4.2)#)<br />
</cfoutput>

Here we see variables and functions both being inserted into the HTML rendered by the browser:

Additional functions

We have looked at creating a variable by using the value of another variable and changing the values of variables. You will find everything from modulo functions to geometry functions standard for mathematic calculations. You will also find a rich number of string functions that can help you process your pages.

What additional types of things can you do with strings? Let's take a look at a piece of code that will help answer that question:

<!--- Example: 1_5.cfm --->
<!--- Processing --->
<cfscript>
  myQuestion = "This is my question.";

  myQuestion = myQuestion & " Is THIS a string?";
  location = find("this",myQuestion);
</cfscript>

<!--- Content --->
<cfoutput>
  myQuestion is (#myQuestion#)<br />
  Location of "this" is (#location#) characters from the start of the string.<br />
</cfoutput>

Here we see results that let us know our search was not successful:

Find and FindNoCase

You might be curious as to why the find function returned zero rather than the actual position. If you look, you will notice that we changed the word this to all capitalized letters. Computers see capitalized letters differently than lowercase letters, so THIS provided an excellent way to make a point on strings being case-sensitive. Let's see how we can find it either way with a built-in ColdFusion function:

<!--- Example: 1_6.cfm --->
<!--- Processing --->
<cfscript>
  myQuestion = "This is my question.";

  myQuestion = myQuestion & " Is THIS a string?";
  location = findNoCase("this",myQuestion);
  location2 = findNoCase("this",myQuestion,location+1);
</cfscript>

<!--- Content --->
<cfoutput>
  myQuestion is (#myQuestion#)<br />
  Location of "this" is (#location#) characters from the start of the string.<br />
  The second "this" is located at position (#location2#).<br />
</cfoutput>

In the following screenshot, we can see that the search function was a success and realize the importance of being case-sensitive aware in text searching:

We did a couple of things here. First, we made our string search case-insensitive. Then, we added a second search to see if there were any more occurrences of the searched for item in the string variable. We added +1 to the location to make sure we skipped that location in our search. It still returns the location based on the start of the string no matter how deep we start in our search. Here is the structure of the function so you can understand how function documentation works when you look things up:

FindNoCase(substring, string, [start])

The arguments of the functions are required unless they are wrapped in [ ]. This function searches for a substring inside of the string optionally starting at start number of characters deep into the string.

Tip

Do not confuse the [ ] for the array notation. When looking at code docs, this only means an optional parameter.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image