# wordwrap with negative width

- Thread: https://externals.io/message/96204
- Original archive: https://news-web.php.net/php.internals/96204
- Started by Stanislav Malyshev on 2016-10-04T02:20:40+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #96204 — Stanislav Malyshev — 2016-10-04T02:20:40+00:00

URL: https://externals.io/message/96204#96204

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

---

## #96205 — Yasuo Ohgaki — 2016-10-04T02:43:59+00:00

URL: https://externals.io/message/96204#96205 · In reply to: #96204

> 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

---

## #96208 — Niklas Keller — 2016-10-04T07:19:33+00:00

URL: https://externals.io/message/96204#96208 · In reply to: #96205

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:
> > […]
>

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.
> +1
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #96243 — Stanislav Malyshev — 2016-10-04T21:34:58+00:00

URL: https://externals.io/message/96204#96243 · In reply to: #96208

Hi!

> What does drop mean? Return false and issue a warning? Keep current

Yes, false + warning.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #96326 — Niklas Keller — 2016-10-12T12:43:00+00:00

URL: https://externals.io/message/96204#96326 · In reply to: #96243

Stanislav Malyshev <smalyshev@gmail.com> schrieb am Di., 4. Okt. 2016,
23:34:

> Hi!
>
> > […]
>
> Yes, false + warning.
>

Sorry for the late reply. Why not keep the current behavior + warning?

--
> Stas Malyshev
> smalyshev@gmail.com

---

## #96335 — Stanislav Malyshev — 2016-10-13T05:13:26+00:00

URL: https://externals.io/message/96204#96335 · In reply to: #96326

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

---

## #96350 — Yasuo Ohgaki — 2016-10-13T19:39:51+00:00

URL: https://externals.io/message/96204#96350 · In reply to: #96335

Hi Stas, Niklas,


> > […]
>
> 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

---

## #96379 — lauri.kentta@gmail.com — 2016-10-15T11:34:33+00:00

URL: https://externals.io/message/96204#96379 · In reply to: #96350

> Hi Stas, Niklas,
> 
> On Thu, Oct 13, 2016 at 2:13 PM, Stanislav Malyshev 
> <smalyshev@gmail.com> wrote:
> > […]
> 
> 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ä

---

## #96385 — Stanislav Malyshev — 2016-10-16T00:28:52+00:00

URL: https://externals.io/message/96204#96385 · In reply to: #96379

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

---

## #96386 — lauri.kentta@gmail.com — 2016-10-16T08:01:22+00:00

URL: https://externals.io/message/96204#96386 · In reply to: #96385

> Hi!
> 
> > […]
> 
> 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?

> > […]
> 
> 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ä

---

## #96388 — Yasuo Ohgaki — 2016-10-16T09:22:38+00:00

URL: https://externals.io/message/96204#96388 · In reply to: #96386

Hi all,


> Zero width doesn't make sense either, so are you going to remove that as
> well?

0 could be included. IMHO.
Frankly, I think it should because string could be converted to 0.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net
