1

We write PHP code inside the HTML code via <?php ... ?> tags. So normally it would not make sense to write HTML code inside PHP code that is already inside HTML code, if you can just exit the PHP for the lines you need. But what if you need the HTML code in the same line as you have the PHP code?

My example would go like this:

<div>
    <?php ($bool) ? <script>...</script> : <script></script> ?>
</div>

Is this:

<div>
    <?php if($bool): ?>
        <script>...</script>
    <?php else: ?>
        <script>...</script>
    <?php endif; ?>
</div>

the only way?

Note: instead of <script> you could have <h1>, <strong>, <title> or any other "one-liner".

Thank you in advance.

s3c
  • 1,481
  • 19
  • 28
  • 1
    Put your html into php strings and echo them. Or do something like the second snippet. – Sergio Tulentsev Apr 01 '18 at 21:36
  • 1
    There are multiple ways to do this. See http://php.net/manual/en/language.types.string.php for a long list. Why does it have to be `one-liner`? – chris85 Apr 01 '18 at 21:41
  • Yes, that worked perfectly. `
    ` \n \t `..." : ""; ?>` \n `
    ` did the job.
    – s3c Apr 01 '18 at 21:58

4 Answers4

1

In order to print any string into your html code from PHP snippets use echo function.

http://php.net/manual/en/function.echo.php

Konrad Albrecht
  • 1,701
  • 1
  • 9
  • 20
1

So you just need to add echo

<div>
<?php 
    if($bool) {
     echo '<script>...</script>';
    } else {
     echo '<script>...</script>';
    } ?>

mooga
  • 3,136
  • 4
  • 23
  • 38
1

Sure, alternative syntax would be the way to go when you have multiple lines of HTML, as you already stated...

However, for one liners, you can shorten <?php echo '...' ?> with <?= '...' ?> and wrap your HTML within single or double quotes, depending if you are already using double quotes within your HTML syntax. You may also escape them if you like, but that'd be messy.

<div>
    <?= ($bool) ? "<script>...</script>" : "<script></script>" ?>
</div>
dvlden
  • 2,402
  • 8
  • 38
  • 61
  • Best answer for the shortest code. Thank you! Is there any benchmark (dis)advantage to using `=` instead of ` – s3c Apr 02 '18 at 06:07
  • @s3c No, I don't think so. I never really bothered even searching for something like that, but it's just a short syntax, so shouldn't be making any difference to performance at all. – dvlden Apr 02 '18 at 15:20
  • So if php is executed on a linux based server, it will go through just the same? Asking because I was told I should never use just `` instead of ` – s3c Jan 16 '19 at 10:08
  • `` is the shorthand php open tag, but `=` is not the same as the ``. `=` is equivalent to ` – ontrack Aug 25 '21 at 08:27
0

Stumbled upon this and decided to answer my own question just to point other newbies in the right direction.

Important note: Nowadays I'm using Laravel Framework and if you don't know it, you should definitely get to know it (there are alternatives though).

But I started following MVC architecture strongly even before that. So even before Laravel's Blade templates, my views looked something like the following.

<html>
<body>
    <?php if ($isUserAuthenticated) : ?>
        <div>
            <span>Welcome <?= $username ?>
        </div>
    <?php else : ?>
        <a href="login">Login</a>
    <?php endif ?>
</body>
</html>

As you can see there is absolutely no data manipulation in the view.

I also tried my best not to store any HTML strings into variables, but sometimes it makes for less code, so I did something like the following.

$alert = match($errorCode) {
  1 => <<<HTML
    <div class="alert alert-danger">Big error</div>
    HTML,
  2 => <<<HTML
    <div class="alert alert-warning">Small error</div>
    HTML,
  default => ""
};

That way I can keep syntax highlighting (in VSCode) for HTML.

Note: match expression is new in PHP8, but you could achieve the same before with a switch statement.

s3c
  • 1,481
  • 19
  • 28