Apply class to a link within an echo?

Asked

Viewed 571 times

0

I have this echo below and would like to apply the class buttonlink in it, but I’m not getting.

I don’t know if it’s the quotation marks, that backslash or if it’s not possible to do that, if anyone can help.

echo "<a href=\"" . $pasta . $resultado["nomearq"] . " class="buttonlink"\">".$resultado["dataup"]." / " . $resultado["nomearq"] . "</a><br />";
  • It’s because the $

  • Ah, wait. You want "buttonlink" to be literal?

  • @LINQ As so literal?

  • 1

    Literal, man. Literal.

  • @LINQ I want to change the color and font size that comes out when loading the page, understand? Applying the class buttonlink

  • Arthur, I noticed that you tried to mark several answers as correct. This can only be done in one reply, ok?

  • @LINQ I thought I could, but I marked yours for being the first to answer!

  • 1

    Just for the record, the downvote is not mine, even so I voted to close because this is the basics of PHP and has been debated here, I think the best question to understand this is this https://answall.com/q/4652/3635 that speaks of the characteristic of language. Observing: the answers are all correct, but I want to make it clear although there are no significant differences in the use of simple quotes in HTML, the question is more that you may get confused if mix different use, but it’s just a hint, nothing serious.

Show 3 more comments

4 answers

5


The quotation marks must escape.

Even, it’s almost right in the original code, but there’s a double quotation mark left and there’s no escape in the first double quotation mark.

To correct can do only this:

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

Or, better organize your code and leave so:

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

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

It’s also possible to make a maneuver using single and double quotes, but I’m not even going to leave an example of this because it turns into horrible code.

  • Thank you very much man, vlw too much, helped a lot!!

3

Yes, the problem is the quotation marks in the middle of your string. I would particularly recommend you to use the 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 is much more readable. Or even:

<?php

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

echo "<a href='{$href}' class='buttonLink'>{$label}</a><br>";
  • 1

    PHP has almost implicit string interpolation and you using printf. Oh no =D

  • Thank you so much Anderson, you helped so much, it worked out here!!

3

Try using simple quotes, it’s easier.

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

    Thank you very much, I always get lost in these single quotes and double kkk I have to get used to yet!

0

Follows:

The result is the same in both, but if you use echo it tries to 'printar' as an element and only the word appears with the link.

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>';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.