Solved

Coldfusion checking if variable has a value

Posted on 2011-02-26
8
346 Views
Last Modified: 2012-05-11
Hi,
I need to check if a variable has a value in it. If it does, display out, if not, ignore it.

I tried this:
<cfif #name#>
Dear #name#,<br/>
</cfif>

The response I got back was that value "Mike Smith" can't be converted to boolean.
What is the correct syntax for checking something like this?

Thanks!

0
Comment
Question by:nachtmsk
  • 4
  • 3
8 Comments
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 34987646
<cfif isDefined(name) and name NEQ ''>
         Dear #name#,<br/>
</cfif>

Open in new window

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 34987661
try this, it needs to be enclosed in string..

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fbd.html

<cfif isDefined("#name#") and #name# NEQ ''>
         Dear #name#,<br/>
</cfif> 

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 34987799

Neither of those is correct, you need to put quotes around the variable name.  The variable name must be in quotes because it might not exists.

<cfif isDefined("name")>
  Dear #name#,<br/>
</cfif>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 39

Expert Comment

by:gdemaria
ID: 34987816

But what if the value is empty?   I don't think you want to say   Dear  ,  

So you want to test if it exists and if it is empty...

<cfif isDefined("name") and len(name)>
   Dear #name#,
</cfif>



One more method is to just create the variable if it doesn't exist using cfparam,  that will allow you to use it for the rest of the page without always testing if it exists...

<cfparam name="name" default="">

<cfif len(name)>
   Dear #name#,
</cfif>


0
 
LVL 11

Accepted Solution

by:
Brijesh Chauhan earned 500 total points
ID: 34987862
okay the idea is to use isDefined function to check the existence of variable, thanks for correcting the syntax.

you can define name and then check if it is not blank or not

<cfparam name="name" default="" />

<cfif isDefined("name") and #name# NEQ ''>
         Dear #name#,<br/>
</cfif>

Open in new window

0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 34989980
I am bit late to come here, All the syntax specified by gd and brij are correct and you can use any one and it will work!

Like this:

<cfparam name="name" default="">

you can do the following!

<cfset isName = isDefined('name') and name NEQ ''>


the isName variable with return you the value yes/no in this case and ur purpose solved and this is reusable if you want to use it in multiple scenarios in page


<cfif isName>
Welcome #name#
<cfelse>
Welcome Yourname
</cfif>
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 34990037
<cfparam name="name" default="" />  <!---- this makes sure the variable exists

<cfif isDefined("name") and #name# NEQ ''>  <!------ there is no point in using IsDefined() here because you KNOW the variable exists from previous line, also there is no need for the #s in the CFIF statement
         Dear #name#,<br/>
</cfif>


>  All the syntax specified by gd and brij are correct

NOT TRUE.  The ones in brij's first posts will cause an error and are not correct

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35096054
Did you choose that answer randomly?

You don't need to check if a variable is define if you just used cfparam to ensure it exists


<cfparam name="name" default="" />  <!===== you define it here

<cfif isDefined("name") and #name# NEQ ''>  <!==== here you check to see if it's defined
      Dear #name#,<br/>
</cfif>


<cfparam name="name" default="" />  
<cfif len(name)>  <!==== don't use # inside a cf statement, len() is faster than NEQ
  Dear #name#,<br/>
</cfif>

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now