CFML - A Dynamic Language - What does it mean?
by Gavin Pickin - Dec 07, 2013
Page 1 of 1
CFML is known as a dynamic language, variables are dynamically typed so they are evaluated at runtime to see what type of value they are, because, you can reuse variables, as numbers and strings, whoever you see fit. At first it might seem strange, in time you will see its power.
CFML is a dynamically typed language, this means when we create the variables, they might be a certain type, like numbers, or strings, but when we go to use them, at runtime, the CFML server dynamically figures out how you intend to use the variables, and use them that way. For example, we will set 3 variables. number1 is the number 5 number2 is the number 6 number3 is the number 7 Then we're going to use the + operator to add number1 and number2. What will happen... they're both numbers, so the CFML server will add the numbers and store the answer in the variable answer1. Then we're going to use the + operator to add number1 and number3. Now, number1 is a number, number3 is a string, will this thrown an error? In strictly typed languages, yes it would... but, CFML is dynamic. So, when CFML sees a number + string, CFML automatically tries to convert the string to a number. So in this case... "7" gets changed into 7 just for the equation... but is still "7" stored in the number3 variable. Run the code below and see for yourself.
Since CFML is a dynamic language, the language has some built in tools for checking to see if a variable is a certain type. For example, you can check to see if a variable is a number, with this function isnumeric(variabletocheck) So lets check out some variables, and see if they are numbers or not. Below, we have 6 numbers, all possibly numbers... and we have 3 tests to see if the number is really a number or not. See if you can guess right with the first three, run and code and see if you are right. Next, add 3 more cfif statements to see if the next 3 numbers are really numbers, and run the code to test your best guess.
I bet you thought that was pretty interesting. Eight is not a number, because CFML doesn't understand strings as numbers, but true on the other hand, a boolean, can be represented as a number. Wait a minute, didn't true come back as false... not a number? Yes, you're right, but we can CAST it to a number... by using the INT function int(theNumberToCast) Lets see what happens now
When you ran that code... you'll see that number1 is stored as a 1, and it tests as a number, because we cast true when setting the variable. Number2 or false, is a 0, and it tests as a number, because we cast it when setting the variable. Number3 is true, it is not a number, because we did not cast it when setting the variable, or testing for it. Number4 is true, because we did not cast it when setting the variable, but this tests as number because when we check if its a number, we cast the value in the cfif statement. There is a lot of flexibility with dynamic languages, which can be great, but you have to remember when and what casts, and when to do it. Thats all for this basic lesson on what makes CFML a dynamically typed language. I will be adding another dynamic typing tutorial soon which looks more closely as booleans, and dates, which can be very interesting. For now, try playing below, try entering different values for number1 and run the code to see what is and isn't a number. Thanks for reading this tutorial, enjoy the rest of the tutorials, and welcome to the world of CFML.