wordwrap with negative width

php.internals

Stas Malyshev

9 years ago
Hi! I've just discovered that wordwrap() (http://php.net/wordwrap) accepts negative width parameter and treats it as if it was 0. It looks like a bug, never documented and works in code only by accident as far as I can see. So, my question is - would it be OK to drop support for negative width in wordwrap?
-- Stas Malyshev smalyshev@gmail.com

Yasuo Ohgaki

9 years ago
On Tue, Oct 4, 2016 at 11:20 AM, Stanislav Malyshev <smalyshev@gmail.com> wrote:
> I've just discovered that wordwrap() (http://php.net/wordwrap) accepts > negative width parameter and treats it as if it was 0. It looks like a > bug, never documented and works in code only by accident as far as I can > see. So, my question is - would it be OK to drop support for negative > width in wordwrap?
It should be a user script bug. +1
-- Yasuo Ohgaki yohgaki@ohgaki.net

Niklas Keller

9 years ago
Yasuo Ohgaki <yohgaki@ohgaki.net> schrieb am Di., 4. Okt. 2016, 04:43:
> On Tue, Oct 4, 2016 at 11:20 AM, Stanislav Malyshev <smalyshev@gmail.com> > wrote: > > I've just discovered that wordwrap() (http://php.net/wordwrap) accepts > > negative width parameter and treats it as if it was 0. It looks like a > > bug, never documented and works in code only by accident as far as I can > > see. So, my question is - would it be OK to drop support for negative > > width in wordwrap? >
What does drop mean? Return false and issue a warning? Keep current behavior and issue a warning? Regards, Niklas It should be a user script bug.

Stas Malyshev

9 years ago
Hi!
> What does drop mean? Return false and issue a warning? Keep current
Yes, false + warning.
-- Stas Malyshev smalyshev@gmail.com

Niklas Keller

9 years ago
Stanislav Malyshev <smalyshev@gmail.com> schrieb am Di., 4. Okt. 2016, 23:34:
> Hi! > > > What does drop mean? Return false and issue a warning? Keep current > > Yes, false + warning. >
Sorry for the late reply. Why not keep the current behavior + warning?
--

Stas Malyshev

9 years ago
Hi!
> Yes, false + warning. > > > Sorry for the late reply. Why not keep the current behavior + warning?
Because current behavior makes no sense, negative length is not really something that exists.
-- Stas Malyshev smalyshev@gmail.com

Yasuo Ohgaki

9 years ago
Hi Stas, Niklas, On Thu, Oct 13, 2016 at 2:13 PM, Stanislav Malyshev <smalyshev@gmail.com> wrote:
>> Sorry for the late reply. Why not keep the current behavior + warning? > > Because current behavior makes no sense, negative length is not really > something that exists.
I agree Stas. If we consider compatibility, keeping nonsense behavior is better. However, who sets negative offset and make use of it? Compatibility is important, but this current behavior is not important, IMO. Regards,
-- Yasuo Ohgaki yohgaki@ohgaki.net

Lauri Kenttä

9 years ago
On 2016-10-13 22:39, Yasuo Ohgaki wrote:
> Hi Stas, Niklas, > > On Thu, Oct 13, 2016 at 2:13 PM, Stanislav Malyshev > <smalyshev@gmail.com> wrote: >>> Sorry for the late reply. Why not keep the current behavior + >>> warning? >> >> Because current behavior makes no sense, negative length is not really >> something that exists. > > I agree Stas. > > If we consider compatibility, keeping nonsense behavior is better. > However, who sets negative offset and make use of it? > Compatibility is important, but this current behavior is not important, > IMO.
Keeping the current behavior doesn't really cost anything. Why change it? It's very easy to imagine a script like this: <?php $text_width = $parent_width - $other_width - $margin; echo wordwrap($text, $text_width); ?> Here someone tries to calculate $max_width dynamically. The calculation might produce a negative result sometimes. The text would be visible, even if it's not word wrapped. It might not look as expected, but it would be readable.
-- Lauri Kenttä

Stas Malyshev

9 years ago
Hi!
> Keeping the current behavior doesn't really cost anything. > Why change it?
I think I answered this question several times, including in the email you are quoting! If you disagree, please state the matter of your disagreement, repeating the same question over and over does not help.
> It's very easy to imagine a script like this: > > <?php > $text_width = $parent_width - $other_width - $margin; > echo wordwrap($text, $text_width); > ?> > > Here someone tries to calculate $max_width dynamically. > The calculation might produce a negative result sometimes. > The text would be visible, even if it's not word wrapped. > It might not look as expected, but it would be readable.
If it was expected to be wrapped but is not, then it would be shown on top of other elements, or break the design layout. Not exactly the best idea. Since calling it with negative argument makes no sense, there's no behavior that makes sense. Producing an error means this nonsense situation is discovered.
-- Stas Malyshev smalyshev@gmail.com

Lauri Kenttä

9 years ago
On 2016-10-16 03:28, Stanislav Malyshev wrote:
> Hi! > >> Keeping the current behavior doesn't really cost anything. >> Why change it? > > I think I answered this question several times, including in the email > you are quoting! If you disagree, please state the matter of your > disagreement, repeating the same question over and over does not help.
To be clear: I'm not agains the warning, I'm against the false. I don't seem to find any other "answer" than your personal opinion that the old behavior "doesn't make sense". That's not a real answer. Zero width doesn't make sense either, so are you going to remove that as well? There are a lot of things in PHP that don't make sense, and you can't remove them all. I'll rephrase the question: Suppose you add the warning anyway. What do you gain by returning false? Is there an additional benefit?
>> It's very easy to imagine a script like this: >> >> <?php >> $text_width = $parent_width - $other_width - $margin; >> echo wordwrap($text, $text_width); >> ?> >> >> Here someone tries to calculate $max_width dynamically. >> The calculation might produce a negative result sometimes. >> The text would be visible, even if it's not word wrapped. >> It might not look as expected, but it would be readable. > > If it was expected to be wrapped but is not, then it would be shown on > top of other elements, or break the design layout. Not exactly the best > idea. Since calling it with negative argument makes no sense, there's > no > behavior that makes sense. Producing an error means this nonsense > situation is discovered.
If it was seriously breaking their layout, they would fix it anyway. Or they would say, "oh well, at least the text is readable!" So let's suppose it currently works "well enough". You get your patch in, and suddenly some text magically disappears. They might not see the warning (old scripts often disable warnings), so they might not know at all why the text is gone. They just tried to upgrade PHP (or their sevice provider did), and now it's broken. OTOH, if you only add the warning, some people may even fix their code, others might not, and it doesn't really matter. Also, if/when you document that negative width is no longer supported, soon people will discover the easy fix, max($width,0), and you have really gained nothing but more hacks in bad code.
-- Lauri Kenttä