您如何在PHP中使用反斜杠制作字符串? [英] How do you make a string in PHP with a backslash in it?

查看:65
本文介绍了您如何在PHP中使用反斜杠制作字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要反斜杠才能成为字符串的一部分.我该怎么办?

I need a backslash to be a part of a string. How can I do it?

推荐答案

当反斜杠\不能转义字符串的终止引号或以其他方式创建有效的转义序列(用双引号引起来的字符串)时,可以使用以下任意一个产生一个反斜杠:

When the backslash \ does not escape the terminating quote of the string or otherwise create a valid escape sequence (in double quoted strings), then either of these work to produce one backslash:

$string = 'abc\def';
$string = "abc\def";
//
$string = 'abc\\def';
$string = "abc\\def";

转义下一个字符会导致解析错误(字符串的引号终止)或有效的转义序列(在双引号中的字符串),则需要转义反斜杠:

When escaping the next character would cause a parse error (terminating quote of the string) or a valid escape sequence (in double quoted strings) then the backslash needs to be escaped:

$string = 'abcdef\\';
$string = "abcdef\\";

$string = 'abc\012';
$string = "abc\\012";

这篇关于您如何在PHP中使用反斜杠制作字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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