Do I need to escape backslashes in PHP?

Viewed 57618

Do I need to escape backslash in PHP?

echo 'Application\Models\User'; # Prints "Application\Models\User"
echo 'Application\\Models\\User'; # Same output
echo 'Application\Model\'User'; # Gives "Application\Model'User"

So it's an escape character. Shouldn't I need to escape it (\) if I want to refer to Application\Models\User?

6 Answers

If it is to be used in an HTML page, then you might as well use HTML character code \ to represent a backslash, like so:

echo 'Application\Models\User';

It will print:

Application\Models\User

Reference:

Related