Jump to content

Whitespace at the end of printed/echoed text


Bike
 Share

Recommended Posts

Hello everyone,

I encountered a strange behavior when I do something like this (which I actually do all the time):

<p><? print $page->something; ?></p>

What happens is that there is a whitespace at the end which is selectable at least in FF and visible in most other browsers when selected.

You can find it on the PW website as well, e.g. headline on this page ("About ProcessWire "): https://processwire.com/about/It must have something to do with PW because I cannot find that bug anywhere else. I thought it might have something to do with how the code is formatted but it proved wrong. The whitespace is of course not in the variables itself since it even happens with print "hello";

It's not a big thing but it annoys me anyway...

Bike

Link to comment
Share on other sites

What type of field is "something"? I am not seeing any whitespace issue here with a text field.

Also, that code as you have it is a bit weird and might not actually work at all on some systems.

You either want the opening php tag to be: <?php

or, you want to use like this:

<p><?=$page->something?></p>

 

Link to comment
Share on other sites

In PHP 8.0 the deprecation notice is converted into a parse error:

  • If short_open_tag is enabled, the use of <? is a parse error.
  • If short_open_tag is disabled, <? has no special meaning (as before).

In PHP 9.0 support for short_open_tag is dropped entirely:

  • <? never has special meaning, it is always interpreted as plain text.

This deprecation and removal plan avoids unintentional code leaks during PHP version upgrades.

https://wiki.php.net/rfc/deprecate_php_short_tags_v2

As for echo vs print - I am not suggesting one over the other - just that you shouldn't use either in a <? shorttag because it won't be available soon. <?= is still OK though.

As for the whitespace, here is a test I just tried:

image.png.07e59bcdae6e25b7e47926e693918fdc.png

image.png.a791760bef2f494587f8f4be83985ee3.png

Now, I do see a whitespace character after the "11111" but that is after the </p>

I don't see any whitespace where it shouldn't be. Perhaps you can do a similar test with screenshots to show us what you are seeing.

 

  • Like 2
Link to comment
Share on other sites

Are the tags in your HTML on separate lines? If so, Firefox will insert a space.

<p>
Firefox will add a space at the end of this.
</p>
<p>But not at the end of this.</p>

I haven't noticed this happening in other browsers, though perhaps I've missed something. I have a memory that Firefox has done this for many years at least, but I'm not sure.

Note that the Inspect tool in Firefox Web Developer Tools re-formats the HTML for easy reading, repositioning tags for many elements, and it removes spaces before tags in at least some cases (which is not helpful and a bit strange in my opinion!), so it's not good for debugging such things. The Page Source tool is more useful.

I note that the heading "About ProcessWire" does actually have a space at the end in the HTML source. This could be for various reasons - such as a Text Formatter, or simply somebody typing a space!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...