Home  >  Article  >  Backend Development  >  讨厌的字符串的写法

讨厌的字符串的写法

WBOY
WBOYOriginal
2016-06-23 13:39:36925browse

如果有一个字符串,我想把它赋给一个变量应如何写,这个字符串很长哈

变量是$txjs

字符串是
onMouseOut="ycxl('xla');" onMouseOver="xsxl('xla',getPos(this,'Top')+16,getPos(this,'Left'));"

我这么写报错,不知为啥
$txjs='onMouseOut="ycxl('xla');" onMouseOver="xsxl('xla',getPos(this,'Top')+16,getPos(this,'Left'));"'

下面是报的错误
Parse error: syntax error, unexpected 'xla' (T_STRING) in D:\WWW\dgcms\e\data\tmp\dt_templist6.php on line 159


回复讨论(解决方案)

所有语言都是一样的,为了避免出现歧义
用双引号括起的字符串中的双引号 和 用单引号括起的字符串中的单引号
都需要转义
php 约定的转义符是 \

php 还提供了自定义定界符,可省去转义的麻烦

$txjs =<<< JSonMouseOut="ycxl('xla');" onMouseOver="xsxl('xla',getPos(this,'Top')+16,getPos(this,'Left'));"JS;

我在字符串里面写了一个变量,但是用转义没有输出变量的值,而是直接输出了变量的名了,即输出了它
$cssbh

$txjs='onMouseOut="ycxl(\'{$cssbh}\');" onMouseOver="xsxl(\'{$cssbh}\',getPos(this,\'Top\')+16,getPos(this,\'Left\'));"';


$txjs='onMouseOut="ycxl(\'\{$cssbh\}\');" onMouseOver="xsxl(\'\{$cssbh\}\',getPos(this,\'Top\')+16,getPos(this,\'Left\'));"';写都不行
哪里有错呢

字符串中的 php 变量,只有在以双引号括起时才会被替换成值
这是 php 的约定,请遵守。没有为什么

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn