AKor
AKor

Reputation: 8882

Escaping a complicated string with PHP

I have a string like this:

<form action='php/zoneNotifUnsub.php' id='zoneNotifUnsub' method='POST'>
    <?php
       echo $var;
    ?>
</form>

I want to echo it out with PHP, and have it look exactly as above.

I started with this:

echo '<form action='php/zoneNotifUnsub.php' id='zoneNotifUnsub' method='POST'>
    <?php
       echo $var;
    ?>
</form>';

But the additional single quotes inside are causing me problems. How can I have the exact verbatim output of my string print (so that no variables are parsed and no code is run)?

Upvotes: 0

Views: 118

Answers (4)

Cristian
Cristian

Reputation: 198

Using double quotes could be an option in this case:

echo "<form action='php/zoneNotifUnsub.php' id='zoneNotifUnsub' method='POST'>
    <?php
       echo $var;
    ?>
</form>";

Upvotes: 1

Jorg
Jorg

Reputation: 7250

You can also use double quotes for the things you want to echo.

So: '<form action="php/zoneNotifUnsub.php" id="zoneNotifUnsub" method="POST">'; and so on.

Upvotes: 2

edorian
edorian

Reputation: 38961

Normal Escaping would look like this:

echo '<form action=\'php/zoneNotifUnsub.php\' id=\'zoneNotifUnsub\' method=\'POST\'>
    <?php
       echo $var;
    ?>
</form>';

Escaping every one of the ' with a backslash. To recap how strings work look in the manual page

You could also go with the HEREDOC Syntax that would look a little bit nicer:

echo <<<OUT
<form action='php/zoneNotifUnsub.php' id='zoneNotifUnsub' method='POST'>
    <?php
       echo $var;
    ?>
</form>
OUT;

Upvotes: 3

corsiKa
corsiKa

Reputation: 82559

Try escaping them

echo '<form action=\'php/zoneNotifUnsub.php\' id=\'zoneNotifUnsub\' method=\'POST\'>
    <?php
       echo $var;
    ?>
</form>';

Upvotes: 1

Related Questions