The ColdFusion <cfdump />
tag is giving me much less information than the PHP function var_dump()
.
Is there any other possibility in CF to find out of what type (integer, string etc.) my variable is?
You can see the current (JVM) type of a variable by doing <cfdump var=#getMetadata(var)# /> or simply by accessing getMetadata(var). getName() .
Variables are a standard part of any programming language. A variable can be visualised as a container that stores a value. We can use variables in many circumstances. For example, we could store the user's name inside a variable.
Null settings at server levelIn the ColdFusion Administrator, click Server Settings > Settings. In the page, you there is the option Enable Null Support. If you enable this option, all your applications running in the server becomes null-enabled. You can also update this setting through an Admin API.
Create an array In ColdFusion, you can create arrays explicitly, by using a function to declare the array and then assigning it data, or implicitly by using an assignment statement. You can create simple or complex, multidimensional arrays. This statement creates a two-dimensional array named myArray.
CFML is dynamically typed, so types can change as required.
You can see the current (JVM) type of a variable by doing <cfdump var=#getMetadata(var)# />
or simply by accessing getMetadata(var).getName()
.
Generally, you don't care whether something is a specific type, so much as whether it can be automatically cast to a specific type - for this reason there are functions including isSimpleValue, isNumeric, isDate, and various others.
So if calling isNumeric(string)
returns true, then you know that you can use that variable for mathematical purposes.
For the most part, that's all the average CF developer cares about, and the rest of this answer probably isn't important (but of course might still be interesting).
If you have reason to deal with types directly, you need be aware that Adobe ColdFusion does funny things with types - doing <cfset number = 123 />
results in a string not a numeric type.
Indeed, most simple values in ACF are created as strings, (and then automatically cast to the relevant type when needed).
At the same time, not all simple values are stored as strings - if you do <cfset number = 1 + 1 />
then you get a number instead.
You can see the types used with this code:
<cfset TypeTest = { array = [] , struct = {} , string = "123" , number = 123 , boolean = true , date = Now() , number2 = 1+1 }/> <cfloop item="key" collection=#TypeTest# > <cfoutput><br/> #key# = #getMetadata(typetest[key]).getName()#</cfoutput> </cfloop>
The results of this code in CF10 are like so:
ARRAY = coldfusion.runtime.Array STRUCT = coldfusion.runtime.Struct STRING = java.lang.String NUMBER = java.lang.String BOOLEAN = java.lang.String DATE = coldfusion.runtime.OleDateTime NUMBER2 = java.lang.Double
The reason for this long-winded explanation is that if CF was to show type when you dumped, half the time it would say "string" and not necessarily be very useful.
If you need to know [potential] types then you would need to create your own dump tag/function which makes use of conversion checking functions described above to determine what you are looking at and output the relevant information.
As mentioned in the comment by Busches, Railo (an alternative CFML engine) does show types when you dump, and it shows correct types.
This is because Railo doesn't just convert (almost) everything to a string - it starts with the relevant type, and converts if needed.
To show this, you can run the above code snippet, here are the results of running against Railo 3.3:
ARRAY = railo.runtime.type.ArrayImpl STRUCT = railo.runtime.type.StructImpl STRING = java.lang.String NUMBER = java.lang.Double BOOLEAN = java.lang.Boolean DATE = railo.runtime.type.dt.DateTimeImpl NUMBER2 = java.lang.Double
If you wanted a simple one-word type, you can probably get a sensible value with this:
ListLast(type,'.').replaceAll('Impl$','')
(There is no specific Java->CFML conversion function - you can see how the type labelling is done for cfdump by looking at the relevant source code.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With