PixelPaul
PixelPaul

Reputation: 2767

PHP escaping concatenated string

I have a seemingly easy task, but I'm struggling a bit. I know that I need to escape the quotes, but I cant seem to get the combination correct.

$referringURL = $_SERVER['HTTP_REFERER'];
echo "<a href = ".$referringURL./MyAccount/SearchUser.aspx" class = "back">Return to Search Users page</a>";

Upvotes: 0

Views: 22

Answers (4)

To avoid quotation problems it is possible to separate the main string and the strings to be inserted by using sprintf : each string to insert is represented in the main string by %s, then you add as many strings as %s you have:

<?php
$referringURL = $_SERVER['HTTP_REFERER'];
$s = sprintf( "<a href = '%s' class = '%s'>Return to Search Users page</a>",
              $referringURL . "/MyAccount/SearchUser.aspx",
              "back" );
echo $s;
?>

This method is less confusing when concatenating multiple strings.

Upvotes: 0

moolsbytheway
moolsbytheway

Reputation: 1291

Have fun

$referringURL = $_SERVER['HTTP_REFERER'];
echo "<a href = '".$referringURL."/MyAccount/SearchUser.aspx' class='back'>Return to Search Users page</a>";

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

Forget escaping, use Heredoc

echo <<<HTML
<a href = "$referringURL/MyAccount/SearchUser.aspx" class = "back">
Return to Search Users page
</a>
HTML;

The actual problem in your code is a missing " before ./MyAccount and 3 unescaped " after that

Upvotes: 1

nospor
nospor

Reputation: 4220

It is worth from time to time mix quotes

echo '<a href = "' . $referringURL . '/MyAccount/SearchUser.aspx" class = "back">Return to Search Users page</a>';

Upvotes: 1

Related Questions