Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Variable isDefined AND equals ????

New Here ,
Apr 18, 2007 Apr 18, 2007
What is the correct syntax to say "If the variable is defined and the variable equals x"??

I have this, but it is not working:

<cfif IsDefined("element") and element eq "person">

adflgkjhdslfgjkdflhs

<cfelse>

adflgkjhdflg

</cfif>
754
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 18, 2007 Apr 18, 2007
Not sure what kind of results you are getting but... that looks correct. Off the cuff, the only other thing that I can think of that might cause some issues is if you have any other variables that share the same name.

If so make sure you explicitly define the scope in your conditional. i.e. isDefined("variables.element") and variables.element eq "person" etc.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 19, 2007 Apr 19, 2007
Always scope your variables. Your code will run quicker (because ColdFusion wont have to check what it is first), be easier to read and cause you less problems in the long run.

Mikey
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 19, 2007 Apr 19, 2007
In what way is it not working?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 19, 2007 Apr 19, 2007
LATEST
In addition to the suggestions above, I think you want single quotes around 'element'.

I would tend to do this like so:

<cfif IsDefined('element')>
<CFIF #trim(element)# eq 'person'>
Do something when it equals 'person'
<CFELSE>
What to do if it does not equal 'person'
</CFIF>
<CFELSE>
What to do if 'element' is not defined
</CFIF>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources