From the command line, I can get the home directory like this:
~/
How can I get the home directory inside my PHP CLI script?
I know that this is an old question, but if you are looking for an alternative and easy to replicate method across your application, you may consider using a library installed via composer. I've written a library that combine some of the answer here and make it a library and I'll shamelessly promote it here : juliardi/homedir.
You can install it via composer :
$ composer require juliardi/homedir
And then use it in your application :
Hope this answer help you.