How to Write HTML Code Inside ≪PHP ≫, I Want Write HTML Code Within the PHP Script So That It Can Be Echoed from Backend

How to write html code inside ?php ? , I want write html code within the PHP script so that it can be echoed from Backend

You can do like

HTML in PHP :

<?php
echo "<table>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>".$name."</td>";
echo "</tr>";
echo "</table>";
?>

Or You can write like.

PHP in HTML :

<?php /*Do some PHP calculation or something*/ ?>
<table>
<tr>
<td>Name</td>
<td><?php echo $name;?></td>
</tr>
</table>



<?php /*Do some PHP calculation or something*/ ?>
Means:
You can open a PHP tag with <?php, now add your PHP code, then close the tag with ?> and then write your html code. When needed to add more PHP, just open another PHP tag with <?php.

how to use html codes inside ?php ? ?

Just check below code

<html>
<body>
<?php
if(isset($_POST["email"],$_POST["name"]))
{
echo "your name is".$_POST["name"]."<br>";
echo "your email address is".$_POST["email"]."<br>";
}
else
{
?>
<form action="form.php" method="post">
Name:<input type="text" name="name">
Email:<input type="text" name="email">
<input type="submit">
</form>
<?php
}
?>
</body>
</html>

Writing HTML in PHP file

The PHP Manual says

For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print().

In simple words

If you have a large amount of html do not use echo to print it.
Because all those html will pass through PHP interpreter, and will take some time (maybe negligible) to parse.

Error at converting PHP string in HTML code

Your PHP-Code should look like:

<?php
$htmlCode = '
<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">
</form>
';

?>

How can I echo the actual HTML tags with PHP?

Use htmlentities to render the p tags as text.

echo htmlentities('<p>' . $t . '</p>');
echo '<br>';

Use the PHP_EOL constant to add a new line character the html ouput.

echo '<p>' . $t . '</p>';
echo PHP_EOL;

what is the recommended way to embed html codes inside php codes?

Don't do it that way at all! Use a templating system like Smarty so you can separate your logic from your display code. It also allows you to bring in a designer that can work with html and might not know php at all. Smarty templates read more like HTML than PHP and that can make a big difference when dealing with a designer.

Also, you don't have to worry about your designer messing with your code while doing a find/replace!

Better yet would be to go to a setup like Django or Rails that has clearly delineated code/template setup, but if you can't do that (cheap hosting solutions) then at least use templating. I don't know if smarty is the best thing for PHP, but its far better than both solutions you are proposing.

How to add a id tag in php output?

may be you want something like this -

if (isset($_SESSION['user_login']))
{
echo '<div id="YOUR-ID-HERE">'.$_SESSION['user_login'].'</div>';
}


Related Topics



Leave a reply



Submit