SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    session name which includes a variable

    Code:
    <cfset number=10>
    
    <cfif not isDefined("#evaluate('session.my#variables.number#')#")>
    
        <cfset 'session.my#variables.number#'=0>
    
    </cfif>
    The code above produce ColdFusion Error saying "Variable session.my10 is undefined."

    How can I check whether session.my#variables.number# is defined or not?
    Last edited by dotJoon; Oct 30, 2008 at 05:43.

  2. #2
    SitePoint Evangelist
    Join Date
    Mar 2007
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code above produce ColdFusion Error
    It's because of evaluate(). Evaluate will attempt to return the value of the variable. Instead, pass the variable name to the IsDefined function.

    Code:
    <cfif not isDefined("session.my"& variables.number)>
    	<cfset session["my"& variables.number] = 0>
    </cfif>
    Session is a structure. So you can also use structure functions:

    Code:
    <cfif not structKeyExists(session, "my"& variables.number)>
    	<cfset session["my"& variables.number] = 0>
    </cfif>

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •