A few things you can try:
if (!empty($contactId)) {
In my experience, I often used the last of two solutions, because there were cases when I would expect the variable to have the value 0, which is valid in some contexts. For example, if I have a website for finding drinks and you want to indicate whether the ingredient is non-alcoholic, I would set it to 0 (i.e., IngredientId = 7, Alcoholic = 0).
Mike purcell
source share