php variable in html no other way then: <?php echo $var; ?>

0 votes
asked Jan 27, 2010 by matthy

I work a lot in mixed HTML and PHP and most time I just want solid HTML with a few PHP variables in it so my code look like this:

<tr><td> <input type="hidden" name="type" value="<?php echo $var; ?>" ></td></tr>

Which is quite ugly. Isn't there something shorter, more like the following?

<tr><td> <input type="hidden" name="type" value="$$var" ></td></tr>

This is possible to but you get stuck with the "" (you have to replace them all with '') and the layout is gone

echo "<tr><td> <input type="hidden" name="type" value="$var" ></td></tr>"

Is there anything better?

6 Answers

0 votes
answered Jan 27, 2010 by meagar

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

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

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.

0 votes
answered Jan 27, 2010 by code-burgar

Use the HEREDOC syntax. You can mix single and double quotes, variables and even function calls with unaltered / unescaped html markup.

echo <<<MYTAG
  <tr><td> <input type="hidden" name="type" value="$var1" ></td></tr>
  <tr><td> <input type="hidden" name="type" value="$var2" ></td></tr>
  <tr><td> <input type="hidden" name="type" value="$var3" ></td></tr>
  <tr><td> <input type="hidden" name="type" value="$var4" ></td></tr>
0 votes
answered Jan 27, 2010 by jordan-running

There are plenty of templating systems that offer more compact syntax for your views. Smarty is venerable and popular. This article lists 10 others.

0 votes
answered Jan 27, 2010 by niels-bom

I'd advise against using shorttags, see Are PHP short tags acceptable to use? for more information on why.

Personally I don't mind mixing HTML and PHP like so

<a href="<?php echo $link;?>">link description</a>

As long as I have a code-editor with good syntax highlighting, I think this is pretty readable. If you start echoing HTML with PHP then you lose all the advantages of syntax highlighting your HTML. Another disadvantage of echoing HTML is the stuff with the quotes, the following is a lot less readable IMHO.

echo '<a href="'.$link.'">link description</a>';

The biggest advantage for me with simple echoing and simple looping in PHP and doing the rest in HTML is that indentation is consistent, which in the end improves readability/scannability.

0 votes
answered Jan 27, 2010 by gcb

I really think you should adopt Smarty template engine as a standard php lib for your projects.

Name: {$name|capitalize}<br>
0 votes
answered Jan 26, 2013 by coly-moore

In a php section before the HTML section, use sprinf() to create a constant string from the variables:

$mystuff = sprinf("My name is %s and my mother's name is %s","Suzy","Caroline");

Then in the HTML section you can do whatever you like, such as:

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter