How to put echo inside href inside php tag?

So i have href iside php tags and inside href i want to always use ROOT_URL:

<?php

  <a href="'echo escapeHtml(ROOT_URL . '?view=products');'">Delete</a>

?>

But i get error message: syntax error, unexpected 'echo' (T_ECHO), expecting ';' or ','

How can i still use the href inside php and still use echo to go to link but without issues?

>Solution :

There are two options for you here, which works best depends on your preferences, code style, etc.

First, build the HTML all in PHP:

<?php
echo '<a href="' . escapeHtml(ROOT_URL . '?view=products') . '">Delete</a>';
?>

This can be written more succinctly if this is the only thing in the PHP tag:

<?= '<a href="' . escapeHtml(ROOT_URL . '?view=products') . '">Delete</a>' ?>

Second, leave most of the HTML as HTML and only build what you need to with PHP:

<a href="<?= escapeHtml(ROOT_URL . '?view=products') ?>">Delete</a>

Leave a Reply