Falsey is not an article of female enhancement.
charles.russell — Mon, 07/12/2010 - 06:28
In branching (if statements) and iteration (looping statements like while) it is important to understand how these statements determine whether to branch or continue the loop. This subject concerns both of Drupal's primary programming languages, PHP and JavaScript. What is interesting is that this is not necessarily a boolean(true, false) evaluation. The two states are more accurately called truthy and falsey.
Consider this: if (myvar){ actionFunction(); }
In this if statement the value of myvar may be anything. How does JavaScript and PHP know how to evaluate the statement? This is even more troublesome because both of these languages are not strongly typed. In most cases if myvar has a value the actionFunction would execute, but not in all. So how do you determine if actionFunction would execute? If something is not falsey by elimination it must be truthy. So what is falsey? In JavaScript it is:
- The boolean false the number 0 or string “0”
- NaN (Not a Number, this may a future subject)
- The empty string
- Null
- Variables that have yet to be defined
PHP has similar conditions. In this language falsey is:
- The boolean FALSE as you would expect
- numeric 0 as an integer or a float
- The empty string “” and the string "0"
- An array with no elements
- NULL
- Unset variables
- Simple XML made from empty tags
Now to paraphrase Jack Nicholson - you can handle the truth.
