• Home
  • Contact
blog.biernacki.ca

Kuba's Online Workshop

  • Home
  • Contact

Coldfusion and checking if a variable is Defined

01/26/2011 Coldfusion How To PHP Thoughts & Rants 2 Comments

As I work with Coldfusion 9, I can’t help but keep comparing it to PHP, and I get annoyed by some of the small differences.

One thing that I really have been getting annoyed with lately is checking if a variable is defined.  In most cases you could do a simple check, is it empty? However Coldfusion will puke all over itself if you do an isNull() check on a variable that is ‘not defined’. You really have to know the difference it seems between an empty value, vs a variable that has no defined value (to me they are both null/void).

In PHP, this would be a no brainer to check if it was null / empty by doing a simple string comparisson. (Then again PHP doesn’t care if a variable has been defined before you try to use it, which some people hate, but others find very convenient).

Anyway, on to the example…

Example (if our portlet has no value for renderRequest.getRemoteUser() ):

<cfset raw_userid = renderRequest.getRemoteUser() />

<cfif IsNull(raw_userid)>
   It is Null.<br />
</cfif>

The above would simply error out, as you cannot check something that is undefined. Below is the proper way to handle such a scenario with the approach:

<cfset raw_userid = renderRequest.getRemoteUser() />
<cfparam name="raw_userid" default="0">

<cfinvoke component="core.liferay-functions" method="checkIfLogin" returnvariable="userId">
	<cfinvokeargument name="userid" value="#raw_userid#">
</cfinvoke>
cfparamcoldfusionisDefinedisNull()PHP

Really cool 404 page on GitHub

Google adding Chrome Desktop Notification for Gmail

Categories
  • Android
  • Apple
  • Coldfusion
  • Cool Apps
  • CSS
  • GitHub
  • Hardware
  • How To
  • In the News
  • Javascript
  • Linux
  • PHP
  • RC Hobby
  • Reviews
  • SysAdmin
  • Thoughts & Rants
  • Tinkering
Recent Comments
  • Jim on Fixing ONKYO RC-710m Remote volume issues
  • Tony on Fixing ONKYO RC-710m Remote volume issues
  • Robert Lawrence on Fixing ONKYO RC-710m Remote volume issues
  • Dasen on Fixing ONKYO RC-710m Remote volume issues
  • Ahmed on Fixing ONKYO RC-710m Remote volume issues
RollmyBlog
  • Twitter
  • WebMojo Design
  • Windsor Ultimate
Proudly powered by WordPress | Theme: Doo by ThemeVS.