字符串中的PHP和反斜杠 [英] PHP and backslashes in strings

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

问题描述

 <?php 
// true
var_dump('\\'==='\');

// false
var_dump('\\\\\''==='\\');

// true
var_dump('\\\\''==='\\\');


解决方案

\ 引入了几种类型的转义序列, \\ 是文字\的转义序列。但是,不能解析为转义序列的 \ s也被视为文字\。



Forfor,'\\'代表字符串\,'\\\\' code>代表字符串\\,就像'\\\\'一样。尝试:

  echo'\\\\' - > \\ 

请参阅 http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single


Can anyone tell me what is happening here?

<?php
// true
var_dump('\\ ' === '\ ');

// false
var_dump('\\\\ ' === '\\ ');

// true
var_dump('\\\\ ' === '\\\ ');

解决方案

\ inside a string literal introduces several types of escape sequences, \\ is the escape sequence for a literal "\". But, \s that don't resolve to an escape sequence are also taken as literal "\".

Therefor, '\\ ' stands for the string "\ ", '\\\\ ' stands for the string "\\ ", just as '\\\ '. Try:

echo '\\\\ ';   -> \\ 

See http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single.

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

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