How to Display a PHP Variable into HTML

php variable in html no other way than: ?php echo $var; ?

There's the short tag version of your code, which is now completely acceptable to use despite antiquated recommendations otherwise:

<input type="hidden" name="type" value="<?= $var ?>" >

which (prior to PHP 5.4) requires short tags be enabled in your php configuration. It functions exactly as the code you typed; these lines are literally identical in their internal implementation:

<?= $var1, $var2 ?>
<?php echo $var1, $var2 ?>

That's about it for built-in solutions. There are plenty of 3rd party template libraries that make it easier to embed data in your output, smarty is a good place to start.

echo HTML with php variable

Try this.. run in your php enviroment

<html>
<head>
<style type="text/css">
p
{
text-align: center;
color: red;
}
</style>
</head>
<body>
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
</body>

</html>

How to display a php variable into html

Make your code more concise with jQuery:

(1) Add this in your <head>:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

(2) Create an element where you want the variable to be displayed, and give it an ID, e.g.:

<span id="printHere"></span>

(3) Add this in your JavaScript:

var duration="<?php echo $postduration ?>";
$('#printHere').html(duration);

For your PHP, try the following two options:

<?=$postduration?>

or...

<?php echo $postduration; ?>

PHP echo variable and html together

<?php
$variable = 'testing';
echo <span class="label-[variable]">[variable]</span>
?>

Should be

<?php
$variable = 'testing';
echo '<span class="label-' . $variable .'">' . $variable . '</span>';
?>

show php variable in html heading

use the following

<?php
$a="hi";
$prints =" <html>
<h1> $a</h1>
</html>";
echo"$prints";
?>


Related Topics



Leave a reply



Submit