\w, and the general variable expansion like
$?, I've recently learned that arithmetic expansion is possible as well.
The following code produces a green prompt in normal cases, which turns to red when the last command returned a non-zero code:
PS1='\[\e[$(($? ? 31 : 32))m\]whatever you want\[\e[39m\]'
How does it work? You should recognize the arithmetic expansion
$(( ... )). Inside that,
$? stands for the return code of the last command, and the construct
expr ? expr-then : expr-else works like in C. So the whole arithmetic clause is evaluated as either 31 (if the return code is anything but zero) or 32 (if the return code is zero).
Now the sequence
ESC[31m happens to be interpreted by the terminal as 'switch to red ink', while
ESC[32m means 'switch to green ink'. That's it.
A complete example is then:
PS1='\[\e[$(($??31:32))m\]$? \w\[\e[39m\]: '