討厭的字串的寫法

來源:互聯網
上載者:User
如果有一個字串,我想把它賦給一個變數應如何寫,這個字串很長哈
如
變數是$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 的約定,請遵守。沒有為什麼

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.