在PHP中,我需要转义反斜杠吗? [英] In PHP do i need to escape backslashes?

查看:152
本文介绍了在PHP中,我需要转义反斜杠吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这可能是一个愚蠢的问题,但是我是否需要在PHP中转义反斜杠,我感到很困惑。

  echo'Application\Models\User';打印Application\Models\User 
echo'Application\\Models\\User';相同的输出
echo'Application\Model\'User';给应用程序\Model'User

所以这是一个转义字符,我不应该逃脱它( \ )如果我想参考 Application\Models\User

解决方案

单引号字符串< a>只有转义序列 \\ \'被识别; \ 的任何其他事件都被解释为纯文字。



所以,因为 \\ M \U 不是有效的转义序列,它们被解释为


I think this may be a stupid question, but I am quite confused whether 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 a escape character, shouldn't I need to escape it (\) if I want to refer to Application\Models\User?

解决方案

In single quoted strings only the escape sequences \\ and \' are recognized; any other occurrence of \ is interpreted as a plain character.

So since \M and \U are no valid escape sequences, they are interpreted as they are.

这篇关于在PHP中,我需要转义反斜杠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆