Solved

Coldfusion checking if variable has a value

Posted on 2011-02-26
8
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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