PHP Variable in HTML No Other Way Than: ≪PHP Echo $Var; ≫

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>

Php variable to be shown in a separate HTML file than the php one

<ul>
<li>Some info:  some info</li>
<li>Amount: <?php include 'Script.php'; ?> </li>
</ul>

Note that your echo should also do a </font> to prevent the rest of the page from turning red.

You must rename your HTML file to .php to be able to do this. But if you do that, you might as well use PHP properly. Remove the echo from Script.php and then you can do:

<?php include 'Script.php'; ?>
<ul>
<li>Some info:  some info</li>
<li>Amount: <font color="red" size="5"><?php echo $sum; ?></font> </li>
</ul>

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

How can I echo HTML in PHP?

There are a few ways to echo HTML in PHP.

1. In between PHP tags

<?php if(condition){ ?>
<!-- HTML here -->
<?php } ?>

2. In an echo

if(condition){
echo "HTML here";
}

With echos, if you wish to use double quotes in your HTML you must use single quote echos like so:

echo '<input type="text">';

Or you can escape them like so:

echo "<input type=\"text\">";

3. Heredocs

4. Nowdocs (as of PHP 5.3.0)

Template engines are used for using PHP in documents that contain mostly HTML. In fact, PHP's original purpose was to be a templating language. That's why with PHP you can use things like short tags to echo variables (e.g. <?=$someVariable?>).

There are other template engines (such as Smarty, Twig, etc.) that make the syntax even more concise (e.g. {{someVariable}}).

The primary benefit of using a template engine is keeping the design (presentation logic) separate from the coding (business logic). It also makes the code cleaner and easier to maintain in the long run.

If you have any more questions feel free to leave a comment.

Further reading is available on these things in the PHP documentation.


NOTE: PHP short tags <? and ?> are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option. They are available, regardless of settings from 5.4 onwards.

show php variable in html heading

use the following

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

PHP Code in Other Block Won't Echo Variable

I solved the issue! Thanks for the help everyone, the issue was in the logic in the code.

Inserting PHP variable value in Javascript file

Rather than try to mix PHP into javascript files, here's what I do in the HTML template:

<head>
<script>
var BASEURL = "<?php echo base_url(); ?>";
</script>
<script src="/path/to/my_script1.js"></script>
<script src="/path/to/my_script2.js"></script>
</head>

This sets the variable and allows all your embedded scripts to access it. base_url() would be replaced by whatever method you use to fetch the base url with PHP.



Related Topics



Leave a reply



Submit