Connection strings are single-quoted, e.g.:
user='my user'
So you need:
'host=localhost dbname=$database user=\'demo demo\' password=$password'
.... however, a user named O'Farrell would break that. If you care about this, you must also replace ' with \' (i.e. backslash-escape it) within strings ... then escape it again so PHP doesn't consume the quotes, producing a PHP string like (untested):
'host=localhost user=\'O\\\'Farrell\' password=whatever'
which gets parsed by PHP into the string to send to PostgreSQL's client library:
host=localhost user='O\'Farrell' password=whatever
which in turn parses it into attributes:
host: localhost
user: O'Farrell
password: whatever
You'll want to do that for passwords and other user-supplied strings. Otherwise a malicious user could set an options= directive passing potentially unwanted settings on the connection by using injection from their password string.
If your client library supports it, it may be easier to use libpq's PQoptions based connections, where each option is a separate string. This supported by Python's psycopg2; I don't know if PHP's Pg driver or PDO support it.