proper way to write a php variable with a hyperlink

Can someone tell me the correct way to write this variable.

 $mess .= "<a href=".$_POST['email_site_address'].">".$_POST['email_site_name']."</a>";
Who is Participating?
Hugh McCurdyConnect With a Mentor Commented:
Oh, I see.  I think it works because PC based browsers are able to repair poor HTML syntax.  Don't count on mobile devices to be able to do that.

What I think you want (not being sure I understand) is to have quotes around the address and name.  This is difficult because you need to quotes to comply with PHP syntax.  Fortunately, you can escape your quotes so that they aren't used as quotes in PHP but do get used as quotes in HTML.

I'm not sure I'm typing this correctly but hopefully I'll get it right.  If it doesn't work, please echo $mess to the screen and share it here.

instead of

$mess .= "<a href=".$_POST['email_site_address'].">".$_POST['email_site_name']."</a>";

Open in new window


$mess .= "<a href= \"".$_POST['email_site_address']."\"> \"".$_POST['email_site_name']."\"</a>";

Open in new window

Hugh McCurdyCommented:
I don't understand the question.  Perhaps a question from me will help -- Where do you want to write the variable?
LueyAuthor Commented:
Well actually it works but I do not understand why.
Normally you would write a hyperlink like so right?
<a href="some_site">"some text"</a>

With my variable would I not be missing the "" around the site address after href=
"<a href=".$_POST['email_site_address'].">".$_POST['email_site_name']."</a>
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Ray PaseurConnect With a Mentor Commented:
You might want to learn about PHP string variables, especially the HEREDOC syntax.  I find that HEREDOC makes life very easy!

I would probably go with something like this untested code snippet.

HTH, ~Ray
// INSTEAD OF $mess .= "<a href=".$_POST['email_site_address'].">".$_POST['email_site_name']."</a>";
$e = htmlentities($_POST['email_site_address']);
$n = htmlentities($_POST['email_site_name']);
$mess = <<<ENDMESS
<a href="$e">$n</a>


Open in new window

Hugh McCurdyCommented:
Ray makes a good point about striving for better code.

The var_dump is a very good idea.  Regardless of what you do, if you are still having trouble, please share the var_dump of $mess with us.
If using Ray's code, also the var_dump of $e and $n.
Ray PaseurCommented:
And a var_dump($_POST) might be a helpful and informative step, as well!
LueyAuthor Commented:
Both examples worked but I ended up using the HEREDOC syntax.  Thanks a lot.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.