-4

This solution to concatenate HTML string and variable is not convenient because we need to interrupt the string:

$content = '<meta property="foo" url="' . $url . '" name="' . $name . '">'; 

whereas this one is not handy because we have to escape " by \":

$content = "<meta property=\"foo\" url=\"$url\" name=\"$name\">";

What other solution (not requiring an extra function) would you use that would improve code readability?


Note: not a duplicate of heredoc related questions because here I'm looking for a one-line solution.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 3
    You know you can use a single quote `'` for the HTML attributes... – Tom Udding Dec 01 '17 at 14:12
  • 1
    I'm voting to close this question as off-topic because questions about how to make working code better belong on https://codereview.stackexchange.com/tour – Quentin Dec 01 '17 at 14:13
  • @TomUdding So what would be your solution? – Basj Dec 01 '17 at 14:13
  • @Quentin No this queston is really code related, and codereview is intended for longer code review than just this. – Basj Dec 01 '17 at 14:14
  • (not requiring an extra function) pity sprintf is perfect for this – lloiacono Dec 01 '17 at 14:14
  • 1
    Then it is off-topic because code readability is largely a matter of opinion. – Quentin Dec 01 '17 at 14:15
  • 2
    `$content = "";` and voila, a perfectly readable string. – aynber Dec 01 '17 at 14:15
  • 1
    Just put markup with markup and use a templating engine like e.g. twig – apokryfos Dec 01 '17 at 14:17
  • Possible duplicate of [What is the advantage of using Heredoc in PHP ?](https://stackoverflow.com/questions/5673269/what-is-the-advantage-of-using-heredoc-in-php) – revo Dec 01 '17 at 14:17
  • @revo No, heredoc needs many lines, whereas i'm looking for a one-liner. – Basj Dec 01 '17 at 14:19
  • If you are describing beginning / ending delimiters as *many lines*, you may be right. – revo Dec 01 '17 at 14:20
  • @TomUdding I often see `"` used in HTML attributes, maybe more often than `'` is there a reason (historical, code readibility, etc.)? – Basj Dec 01 '17 at 14:23
  • @Basj It doesn't really matter, it works the same. It is probably someone's decision to choose for one of the two options. I prefer to use `"` for my `echo` and within what I echo `'`. – Tom Udding Dec 01 '17 at 14:25
  • @TomUdding so you do [like this](https://stackoverflow.com/a/47595318/1422096)? – Basj Dec 01 '17 at 14:27
  • 1
    @Basj Yes and I use the curly braces because I can use [complex expressions](https://secure.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex). – Tom Udding Dec 01 '17 at 14:30
  • @TomUdding Thanks. I don't understand why so many downvotes :) About the probable duplicates: Heredoc is clearly another topic, [such questions](https://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string) are clearly another topic because it doesn't deal with single quote / double quotes. – Basj Dec 01 '17 at 14:33

3 Answers3

3

Use single quotes for raw HTML and curly braces around PHP variables. Like so;

$content = "<meta property='foo' url='{$url}' name='{$name}'>";
Mark
  • 691
  • 7
  • 20
  • Thanks! It even seems to work without the curly braces: `$content = "";` – Basj Dec 01 '17 at 14:28
  • One drawback @Mark: if `$name = "I don't know";` it won't work, because the single quote of `don't` will break the code. – Basj Dec 01 '17 at 14:40
  • 1
    This is true - you could get around that by using `htmlentities()`. You should process your variables using this method before passing them into HTML. Something similar to `$url = htmlentities($url, ENT_NOQUOTES)`. [link](https://www.w3schools.com/php/func_string_htmlentities.asp) for more details – Mark Dec 01 '17 at 14:58
-1

I suggest you to use:

$url = ...;
$name = ...;
$content = sprintf('<meta property="foo" url="%s" name="%s">', $url, $name);

which is a very fast way to produce HTML markup keeping a good readability. If you don't want to use additional functions, your only solution is to use double quotes " externally and single quotes ' for HTML attributes.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
-1

You can try heredoc notation:

    $content = <<<DOC
<meta property="foo" url="$url" name="$name">
DOC;
Andrey Yerokhin
  • 273
  • 1
  • 7