Use HTML in PHP

Asked

Viewed 268 times

1

I have the following code that’s not working.

echo "<a href='memberarea.html'><span class="glyphicon glyphicon-lock"></span> Member area</a>"; 

I think I’m not doing well the embedding of HTML in PHP.

3 answers

2


You need to escape the quotation marks or exchange them for simple quotation marks, since you are using quotation marks for echo.

Escaping the quotation marks:

echo "<a href='memberarea.html'><span class=\"glyphicon glyphicon-lock\"></span> Member area</a>"; 

Or:

echo "<a href='memberarea.html'><span class='glyphicon glyphicon-lock'></span> Member area</a>";
  • I like this versatility of PHP being able to use either of the two Double or Single types, but there is a difference between them, Single Quotes understand the string as literal, Example: echo('My $text'); will print the literal string "My $text", and echo("My $text"); will print the contents of the variable, I always use double quotes so I don’t need to break the string or use escapes, but when I have to use html inside php use single quotes, Example: echo("Hello $world"); and echo('<a href="url">link</a>'); note that in the second echo I did not need to escape leaving the code "cleaner".

1

If you use double quotes to define the string, you need to use single quotes in the middle or escape them:

echo "algo \"algo dentro de aspas\" algo";

or

echo "algo 'algo dentro de aspas' algo";

In your case it could be:

echo "<a href='memberarea.html'><span class='glyphicon glyphicon-lock'></span> Member area</a>";

1

Really, the code presented has no need to be written in php, can be written in html even:

<a href='memberarea.html'><span class="glyphicon glyphicon-lock"></span> Member area</a>

But if you want to put php, you must inside a file . php make use of:

-- script html aqui
<?php
    -- codigo php aqui
?>
-- continuação do script html
  • In fact, whenever possible the idea is to separate HTML from PHP, and I like this, but the cases where we need to put HTML inside PHP, for example when you use Templates separating HTML from PHP completely, there in PHP sometimes you need to make an HTML add-on to be displayed in a Template container, so I make a lot of use of simple quotes, example: echo('<table class="myclass"><tr><td>'.$text.'</td><.tr></table>'); Thus avoiding

Browser other questions tagged

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