开发者

Proper placements of quotes and quotations

I need help with my placements of Quotes and Quotations and if someone can briefly explain the rules for formatting in the future. thanks

$lnk = "<br><br><a href='/rmb_twitter/<%mm_id%>/" . $row['i_id'] . "/" . $en['m_uimg'] . "/3/"'><strong>Send to Twitter</strong></a>开发者_JAVA技巧;";


  • Single quoted ' strings are literals, i.e. they are exactly what you write.
  • Double quoted " strings support interpolation and escape sequences, e.g. "$foo\n" substitues the value of variable $foo and \n as a line break.
  • Whatever you start a string with ends the string, quotes within quotes are therefore tricky and need escaping:

    "'"  // means '
    '"'  // means "
    '''  // syntax error
    """  // syntax error
    '\'' // means '
    "\"" // means "
    

Therefore HTML should preferably be written outside PHP to avoid escape hell:

<a href="/rmb_twitter/<%mm_id%>/<?php echo $row['i_id']; ?>/<?php echo $en['m_uimg']; ?>/3/"><strong>Send to Twitter</strong></a>

Or alternatively:

$lnk = sprintf('<a href="/rmb_twitter/<%mm_id%>/%s/%s/3/"><strong>Send to Twitter</strong></a>',
               $row['i_id'],
               $en['m_uimg']);

Or:

$lnk = "<a href=\"/rmb_twitter/<%mm_id%>/$row[i_id]/$en[m_uimg]/3/\"><strong>Send to Twitter</strong></a>";

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


Coding in an editor with good syntax highlighting should've shown the mistake when the color of the text suddenly changed after the double quote after /3/ towards the end. That matched the opening double quote before the /3/, ending the string.

$lnk = "<br><br><a href='/rmb_twitter/<%mm_id%>/" . $row['i_id'] . "/" . $en['m_uimg'] . "/3/'><strong>Send to Twitter</strong></a>";


$lnk = "<br><br><a href='/rmb_twitter/<%mm_id%>/" . $row['i_id'] . "/" . $en['m_uimg'] . "/3/'><strong>Send to Twitter</strong></a>";

You had an excess ".
Essentially:

" maches with a "

' matches with a '

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜