-2

We all know that a quotation mark must have it's partner. But how will I do it if I got 3 consecutive elements that wil use up a quotation mark (PHP echo, HTML and CSS class, Javascript function)? Something like this

echo '
    <tr class="table-body" onclick="showMenu('edit-user-window')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
'; 

The echo's quotation mark breaks at the start of my onclick function. How do I do it?

1
  • Use backward slash before the quotation mark... Commented Jul 31, 2014 at 1:03

6 Answers 6

4

You can simply escape it:

echo ' <tr class="table-body" onclick="showMenu(\'edit-user-window\')">.....';

Notes

The error you are receiving comes because you effectively "break" the string at those points. It is bad concatenation. And by using the \ before those commas ('), you're telling the php interpreter to ignore the character that follows that slash (\) which, in this case is the comma (').

Sign up to request clarification or add additional context in comments.

2 Comments

The first solution would not work as the OP is also using speech marks in the string.
@swiss196 Good spot, Didn't see that. Edited.
0

Skip it using the slash

echo '
    <tr class="table-body" onclick="showMenu(\'edit-user-window\')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
';

Comments

0

You can use \ to escape a character, so your example would look like:

echo '
    <tr class="table-body" onclick="showMenu(\'edit-user-window\')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
'; 

Comments

0

Use backslash like this...

onclick="showMenu(\'edit-user-window\')"

Backslashes are used in PHP to escape special characters within quotes.

Comments

0

Use either a backward slash \ before ' -> \' (also works if you work with strings encapsulated in " -> \")

echo '
    <tr class="table-body" onclick="showMenu(\'edit-user-window\')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
'; 

or use heredoc syntax

echo <<<EOT
    <tr class="table-body" onclick="showMenu('edit-user-window')">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>
EOT; 

Comments

0

when you are using single quotes use only double qoutes in it or vice versa as

echo '
 <tr class="table-body" onclick="showMenu("edit-user-window")">
        <td> <input type="submit" class="delete-but graphic-buts" value="" /> </td>
        <td> <a href="/res/doc.docx" target="_blank"> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>

'

or

echo "
 <tr class='table-body' onclick='showMenu('edit-user-window')'>
        <td> <input type='submit' class='delete-but graphic-buts' value='' /> </td>
        <td> <a href='/res/doc.docx' target='_blank'> User </a> </td>
        <td> Pass </td>
        <td> user </td>
        <td> View-Only </td>
    </tr>

"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.