If someone gains access to the web server, they can gain the credentials necessary to log on to the database.
There is absolutely no way of preventing this, regardless of how you do it. It could be stored encrypted (in which case it would need to be decrypted with a key that was stored on the machine anyway), in compiled form (in which case it could be decompiled) or determined in some other way (e.g. SSL client certificate, which could be stolen and used by an attacker anyway), but the result is the same.
Yes, you can keep them in a file stored outside the web root. Yes - you could keep them in environment variables set by Apache (from a config file which is readable only by root), but an attacker could gain them anyway.
If an attacker had access to the same account (username) as the web server ran under, they could attach a debugger to the web server process and look for the details in memory. Most things are possible.
One of the most secure ways of doing it may be to set them into environment variables from Apache - in a config file readable only by root. But this is still vulnerable if they can modify the php files, get any php code executed in the same context, or view the output of (for example) phpinfo()
Mark