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

PhpHtmlVariables

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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>
MYTAG;

Solution 3 - Php

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

http://www.smarty.net/

Name: {$name|capitalize}<br>

Solution 4 - Php

I'd advise against using shorttags, see https://stackoverflow.com/questions/200640/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.

Solution 5 - Php

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

Solution 6 - Php

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:

<p>$mystuff</p> 

Solution 7 - Php

I was about to use a template engine like Smarty but my use case is very basic (rendering PHP variables in my emails templates), so I created this class... I hope this can help:

class HtmlRender
{
    public function __construct($template, array $data)
    {
        $this->template = $template;
        $this->data = $data;
    }
    private static function fileGetContentsUFT8($file)
    {
        $content = file_get_contents($file);
        return mb_convert_encoding($content, "HTML-ENTITIES", "UTF-8");
    }
    public function render()
    {
        $template = self::fileGetContentsUFT8($this->template);
        foreach ($this->data as $key => $value) {
            $template = str_replace('{{ ' . $key . ' }}', $value, $template);
        }
        return $template;
    }
}

And this is how I use:

 $renderer = new HtmlRender(
            __DIR__ . '/templates/account.create.html',
            [
                'name' => $clientName,
                'email' => $clientEmail
            ]
  );
  $body = $renderer->render();

And inside your Html use the following syntax :

<p>Your name is: <span> {{ name }} </span></p>
<p>Your email is: <span> {{ email }} </span></p>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmatthyView Question on Stackoverflow
Solution 1 - Phpuser229044View Answer on Stackoverflow
Solution 2 - Phpcode_burgarView Answer on Stackoverflow
Solution 3 - PhpgcbView Answer on Stackoverflow
Solution 4 - PhpNiels BomView Answer on Stackoverflow
Solution 5 - PhpJordan RunningView Answer on Stackoverflow
Solution 6 - PhpColy MooreView Answer on Stackoverflow
Solution 7 - PhpYoussef ElmoumenView Answer on Stackoverflow