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 asMyvariable.
)
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.