Playing around with this even more, I can now answer my own question (and show how much of a beginner's question this). It all boiled down to two syntax errors:
- using "and" instead of "&&" as the boolean operator
- using "$pre = """ instead of "!$pre" to check for empty strings.