Apply class to a link within an echo? [duplicate]

1

I have this echo below and would like to apply the class buttonlink to it, however I'm not getting it.

I do not know if it's the quotation marks, this backslash, or if it's not possible to do that even if someone can help.

echo "<a href=\"" . $pasta . $resultado["nomearq"] . " class="buttonlink"\">".$resultado["dataup"]." / " . $resultado["nomearq"] . "</a><br />";
    
asked by anonymous 26.10.2017 / 19:38

4 answers

5

You must escape the quotes.

In fact, it's almost right in the original code, but a double quote remained, and the escape was missing on the first double quote.

To fix it you can do just that:

echo "<a href=\"" . $pasta . $resultado["nomearq"] . " class=\"buttonlink\">".$resultado["dataup"]." / " . $resultado["nomearq"] . "</a><br />";

Or, better organize your code and leave like this:

$link = $pasta . $resultado["nomearq"];
$descr = $resultado["dataup"]." / " . $resultado["nomearq"];

echo "<a href='{$link}' class='buttonLink'>{$descr}</a><br>";

It is also possible to make a maneuver using single and double quotation marks, but I will not leave an example of this because it is a horrible code.

    
26.10.2017 / 19:44
3

Yes, the problem is the quotation marks in the middle of your string . I would particularly recommend you to use printf in this case.

<?php

$href = $pasta . $resultado["nomearq"];
$label = $resultado["dataup"]." / " . $resultado["nomearq"];

printf("<a href='%s' class='buttonLink'>%s</a><br>", $href, $label);

The code becomes much more readable. Or even:

<?php

$href = $pasta . $resultado["nomearq"];
$label = $resultado["dataup"]." / " . $resultado["nomearq"];

echo "<a href='{$href}' class='buttonLink'>{$label}</a><br>";
    
26.10.2017 / 19:46
3

Try to use single quotes, it gets easier.

echo "<a href=" . $pasta . $resultado['nomearq'] . " class='buttonlink'>".$resultado['dataup']." / " . $resultado['nomearq'] . "</a><br />";
    
26.10.2017 / 19:47
0

Follows:

The result is the same in both, but if you use 'echo' it will try to 'start' as an element and only the word with the link will appear.

Using var_dump:

var_dump('<a href="'.$resultado["nomearq"].'" class="buttonlink">'.$resultado["nomearq"].'</a><br>');

Using echo:

echo '<a href="'.$resultado["nomearq"].'" class="buttonlink">'.$resultado["nomearq"].'</a><br>';
    
26.10.2017 / 19:53