proper way to write a php variable with a hyperlink

Posted on 2011-09-28
Last Modified: 2013-12-13
Can someone tell me the correct way to write this variable.

 $mess .= "<a href=".$_POST['email_site_address'].">".$_POST['email_site_name']."</a>";
Question by:Luey
  • 3
  • 2
  • 2
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36753002
I don't understand the question.  Perhaps a question from me will help -- Where do you want to write the variable?

Author Comment

ID: 36756502
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>
LVL 13

Accepted Solution

Hugh McCurdy earned 125 total points
ID: 36770269
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 36791093
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

LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36794598
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.
LVL 108

Expert Comment

by:Ray Paseur
ID: 36796197
And a var_dump($_POST) might be a helpful and informative step, as well!

Author Closing Comment

ID: 36816344
Both examples worked but I ended up using the HEREDOC syntax.  Thanks a lot.

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Time difference 10 35
Modify PHP Code on the Fly? 8 40
php image upload 3 27
html input clean up 3 32
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now