Intro To Variables
A variable is a place holder. That's it. Fairly straightforward in concept. It gets a little more complicated in practice but, really, that's it. We're used to seeing variables in math classes which are usually called X and Y and other mundane things. In programming, we can certainly do that, but more often we use words that are more descriptive.
For example, we might have a variable for the price of a book and call it priceOfBook (remember that document on cases? Here's where it comes in). We might have a billing application and have a variable called dayBillIsDue and, surprise, it's the day that the bill is due. The name of the variable helps to describe what the variable does.
In BoxLang, the <bx:set> tag is used to set or alter variables. For example, <bx:set myDogsName="fido"> creates a variable named myDogsName and sets the value of it to fido. Seems straightforward enough. That means that myDogsName is a variable that contains a string.
Remember how I said that BoxLang was a "typeless" language? That means that I can just as easily make my variable equal a number, a date or anything else without changing any of that syntax. <bx:set dayBillIsDue = now()> will set the variable dayBillIsDue to today. To display the variable we surround it by #s as in #dayBillIsDue# which will display the date and time it was when we set it.
Create a page called myVariables.cfm in the learnbx folder create a new variable named todayDate and set it today's date.
However, notice that it's in the Java TimeStamp format. How do you fix that?
Format the output for todayDate to be more human-readable version of the todayDate variable BUT don't change the underlying variable.
Were you able to figure it out? It quite possibly might be a dateFormatter but that's just a theory.
Which brings up a good point: if a variable can be anything (date, number, string) and we need it to be something in particular, how can we check and see what type of variable it is?
BoxLang (and other languages) give us a way of testing the TYPE of a variable as well as the value of that variable. For example, to test if something is a date you can type #isValid("date",variablename)#. We can see if our todayDate variable is a date by typing #isValid("date",todayDate)# and we get true.
You can see all the isValid possibilities here: https://boxlang.ortusbooks.com/boxlang-language/reference/types
In the myVariables.cfm page, can you add up the price of your 10 books in your Excel Project by using one variable name? Let's call it totalBookCost. Hint: Start with <bx:set totalBookCost=0> and go from there. Think you can do it? Don't forget to output the final amount to the page.