How to add a href link in PHP?

How to add a href link in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Escaping PHP Syntax When Hardcoding External URLs

When you need to redirect a link from an internal resource, like an RSS feed, to an external domain such as www.otherwebsite.com, simply pasting the raw URL string into your HTML often triggers a syntax error. The issue in the example provided isn't about string concatenation; it is about how PHP interprets text inside angle brackets.

In your original code, the line <a href="< ?php echo url::site(); ?>feed/"> works because the content between the quotes is treated as a string that gets parsed for PHP tags. When you attempted to change this to www.someotherwebsite.com, the error occurred because the browser or parser encountered the characters < and > while trying to read them as plain text, but they were nested inside an existing PHP block structure without proper escaping.

To fix this, you must escape the HTML tags that represent the start of your external link. In PHP, a backslash (\) placed before a character tells the parser to treat that character literally rather than as part of the current code structure. The solution is to output the raw URL string with the necessary HTML entities escaped:

<div style="float:right;">
    <a href="<?php echo url::site(); ?>feed/" target="_blank" rel="noopener">
        <img src="<?php echo url::file_loc('img'); ?>media/img/icon-feed.png" style="vertical-align: middle;" border="0">
    </a>
</div>

However, if you want to point directly to an external site like https://www.otherwebsite.com, you should not use the internal routing helper url::site() for the destination. Instead, simply output the full external URL within the href attribute, ensuring the syntax remains valid:

<div style="float:right;">
    <a href="https://www.otherwebsite.com" target="_blank" rel="noopener">
        <img src="<?php echo url::file_loc('img'); ?>media/img/twitter.png" style="vertical-align: middle;" border="0">
    </a>
</div>

The key distinction here is understanding the scope of your URL helpers. Methods like url::site() are designed to generate paths relative to the current application's domain. They expect to be followed by a path segment, such as /feed/. If you try to append a string after url::site() to make it point externally, you risk breaking the routing logic or creating malformed URLs if the helper already includes a trailing slash or protocol.

For external links, it is best practice to hardcode the full URL including the https:// protocol. This ensures that the link works regardless of the current site's configuration and avoids issues with mixed content warnings in browsers. Additionally, adding target="_blank" and rel="noopener" attributes is crucial for security. When you open a new window from an external domain, noopener prevents the destination page from accessing the original window object via window.opener, which mitigates certain types of attacks like tabnabbing.

If your application uses a custom URL generator that forces internal paths, you might need to bypass it entirely for external links. You can achieve this by defining a constant or using a dedicated method in your configuration file. For instance, you could define:

define('EXTERNAL_RSS_URL', 'https://www.otherwebsite.com');

Then reference it in your view:

<a href="<?php echo EXTERNAL_RSS_URL; ?>" target="_blank" rel="noopener">
    <img src="<?php echo url::file_loc('img'); ?>media/img/twitter.png" style="vertical-align: middle;" border="0">
</a>

This approach keeps your external URLs centralized and easy to update without hunting down every instance in your codebase. It also makes the intent of the link explicit, distinguishing it from internal navigation elements that rely on url::site().

When working with legacy applications or older PHP versions, be mindful that some routing helpers might not handle empty strings well. If you attempt to concatenate an external URL to a helper that expects a relative path, the resulting string might be truncated or misinterpreted. Always verify the output of your echo statements in a development environment before deploying changes to production.

In summary, switching from an internal feed link to an external website requires abandoning the relative path generation logic for that specific attribute. By using escaped HTML syntax or defined constants for external URLs, you maintain code integrity and security while achieving the desired redirection behavior.

Our network