Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading