To output a fixed HTML link from PHP, echo an anchor string with compatible quotation marks:
<?php
echo '<a href="https://example.com">Visit the site</a>';
?>
Because the PHP string uses single quotes, the HTML attributes can use ordinary double quotes.
Two correct ways to write the link
Use a single-quoted PHP string
This is usually the simplest option for a short, static fragment:
<?php
echo '<a href="https://example.com">Visit the site</a>';
?>
Use a complete URL such as https://example.com for an external destination, or a deliberate relative path such as /contact.php for an internal page.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Use a double-quoted PHP string
If the PHP string is double-quoted, escape the double quotes used by the HTML attributes:
<?php
echo "<a href="https://example.com">Visit the site</a>";
?>
Write ordinary HTML outside PHP
PHP supports mixed content: everything outside a pair of PHP opening and closing tags is ignored by the PHP parser and sent as page output. For a link that does not need PHP at all, write it directly:
Rank #2
<a href="https://example.com">Visit the site</a>
You can also leave PHP mode around a dynamic value:
<a href="<?php echo htmlspecialchars($url, ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlspecialchars($label, ENT_QUOTES, 'UTF-8'); ?></a>
For large blocks of markup, keeping HTML outside PHP is generally clearer and more efficient than placing the entire block inside echo or print.
Rank #3
Safely output dynamic URLs and labels
Escape HTML output
When a URL or link label comes from a variable, use htmlspecialchars() for the HTML output context. ENT_QUOTES converts both single and double quotes:
<?php
$url = '/profile.php?id=42';
$label = 'View profile';
echo '<a href="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '">'
. htmlspecialchars($label, ENT_QUOTES, 'UTF-8')
. '</a>';
?>
Validate the destination separately
HTML escaping prevents characters in a value from breaking the surrounding markup; it does not determine whether the destination is permitted. Apply an application-specific URL policy as well—for example, allow only known hosts, approved schemes, or generated internal paths when users can influence the value.
Rank #4
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
Common quoting and markup mistakes
- Unescaped nested quotes: a double-quoted PHP string containing
href="..."must escape those inner double quotes, or the PHP string ends early. - Using a bare host name:
www.website.commay not resolve as intended. Usehttps://www.website.comfor an external link when that is the destination. - Putting static HTML in an unnecessary echo: switch back to HTML mode when most of the block is markup.
- Malformed line breaks: use
<br>or<br />where a line break is actually needed;</br>is not a valid closing tag. - Framework translation strings: if the anchor is inside a localization function such as
__(), follow that framework’s rules for trusted HTML and translated content rather than assuming every string is safe to render as markup.
Which approach should you choose?
| Situation | Recommended approach | Reason |
|---|---|---|
| Short, fixed fragment | Single-quoted echo |
Readable quoting with no escaped attribute quotes |
| Short fragment already inside a double-quoted string | Escape the inner double quotes | Preserves the surrounding PHP string |
| Mostly static page markup | Switch out of PHP mode | Clearer and generally more efficient for larger text blocks |
| Variable URL or label | HTML output plus validation policy | Escaping protects markup; validation controls allowed destinations |
The Bottom Line
For a simple link, use echo '<a href="https://example.com">Visit the site</a>';. For larger or mostly static markup, write the anchor directly outside PHP tags. Escape dynamic values for HTML and validate dynamic destinations separately.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →




