0

I have the following code, which is a plain HTML text with some PHP variables. Some of these variables include conditionals, and therefore

The <?= $name; ?> reaches a maxiumun speed of <?= speed; ?> km/h
    <?php if( $turbo ) { ?>
      , <?= $speed + $turbo; ?> if it has a turbo
    <?php } ?>.

This outputs the following:

The viper reaches a maximum speed of 320 km/h , 360 if it has a turbo .

(quick note, the numbers are totally made up).

My problem is with the extra space between "km/h" and the comma, and between "turbo" and the final colon.

If I put everything in a single line the issue is fixed, but I have other sections of the text with much more complex conditions, and putting them all in one line would make the code unreadable. Is there any other way I can get rid of those spaces?

Guillermo Carone
  • 818
  • 2
  • 9
  • 24
  • I don't think there is much more you can do, than to wrap the whitespace into HTML comments, so that it won't show when the result gets interpreted as HTML - https://3v4l.org/OMPa8 If that doesn't suit your needs, then you might need to find a different way to "template" your content. – CBroe Jul 31 '23 at 08:52
  • Can you show us a reproducible example? because browsers strip off spaces anyway. See https://stackoverflow.com/questions/17784595/do-browsers-remove-whitespace-in-between-text-of-any-html-tag – nice_dev Jul 31 '23 at 08:53
  • 2
    you should move those "much more complex conditions" into the business logic part – Your Common Sense Jul 31 '23 at 09:06
  • You can format your code like [**this demo** at onlinephp.io](https://onlinephp.io?s=RYy9CsIwFIX3QN7hDoUqihZ1S5q7OnbwBaLekFDSlrQVQXx38zM4nnO-80nsrh1n1aA9QQv1y00UagHVPBE9Y3M-NTEta7iPMV0awRlnqG6WQGKbfwIVBNIPSzNo8PrtVr8OUAyjyVwOCez90UrkzJlNsW7hk4T7P7YrQ6KdAbeA1Umcy3T9ojr8AA%2C%2C&v=8.2.8) – bobble bubble Jul 31 '23 at 10:22

1 Answers1

1

I mean, you could just "prepare" "the turbo-string" sth. like:

<? $turbo = $turbo ? ', ' . ($speed + $turbo) . ' if it has a turbo' : ''; ?>

The <?= $name; ?> reaches a maxiumun speed of <?= $speed; ?> km/h<?= $turbo; ?>
john Smith
  • 17,409
  • 11
  • 76
  • 117