Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104964 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62689 invoked from network); 27 Mar 2019 16:36:22 -0000 Received: from unknown (HELO mail-it1-f173.google.com) (209.85.166.173) by pb1.pair.com with SMTP; 27 Mar 2019 16:36:22 -0000 Received: by mail-it1-f173.google.com with SMTP id y63so55990itb.5 for ; Wed, 27 Mar 2019 06:30:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=U0necdUhWhxOdieXuOSdrNArlW20ekfLOgEMLNAXR9U=; b=oewIe4xYYPF7u0L6dFJzRPePPlwrFMTt1sYIm2nzko98s9IuFYbjdsSV3Q1OVJHK3y 6itTQ2HE43ZA6bafVjFELGecL6OfRViKJwJEFUNBh9XdSKHEBovd0owf9x2mfX5P/i3U iP6+xMMkpwkaRu7PyO1RXxOi1yhpPiib4uxk3VTvZB5ylSml+mPeXkL1F8Ys788hghNw VjtJoHWPTm9WCNNSjFjeWHClYiqUCU3KKpZSaDnp4v3NLJ7Y6QU4mA4wzEgAOOckrWzN YycPEV8ow+WqYI/UR4OJHM/TDTPqHY9zM3Q+vUc4vxNRZq6qVBkyT0XvhGt0ohPVJhHB kipA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=U0necdUhWhxOdieXuOSdrNArlW20ekfLOgEMLNAXR9U=; b=pVL9Tj//IJ5Vf7DYuclQUYEQzbpYiJd+Leg6UzSi6AKzPWssUKUA2EvumYaV13EMBq YLGJiYFDyPERhIYJjVp0/MQQE6lnpE8f5It8G7rrfjLWzsuyzie06CFtaWO8rch4Jgjz Eh3UTV9bdLDwmEfUJX5G5SSJR18bqDJYreoxDoNdlGOSMWkSIecyEryjfTjw+b7TtDqP 4Z7xY1dqieTj/lZdBJdSRQgtRGSPgIom5a63/fYUkQpOfgVjHQP2FnxIi8SH0l1fo+9e r4HQE69vEphCK+2lSsNzoMDFpDuHU0HRR9/HFUT0WpK1ksFAeTXqm92dKTmm5IRkPt6+ w/8Q== X-Gm-Message-State: APjAAAUPQbUXqP3LnBfkdig+6MV8rxoCroYFkBcOIHvHV5eoxlUvdDlS H/veNQfFfkE420xmiYVta7f7fV/rNvzD+eXT598= X-Google-Smtp-Source: APXvYqwOS4EQIS3bOp+2jZCGR9Z5P8J3KI0LbVI6DEM9TTK57TyPvbtGiROmsgGnhdo174o1ZVHpTcElOxDCZ0ni5xg= X-Received: by 2002:a24:f30d:: with SMTP id t13mr3498763ith.90.1553693400102; Wed, 27 Mar 2019 06:30:00 -0700 (PDT) MIME-Version: 1.0 References: <6C077569-A049-422B-A4CD-6EE5F8DEC388@gmail.com> In-Reply-To: <6C077569-A049-422B-A4CD-6EE5F8DEC388@gmail.com> Date: Wed, 27 Mar 2019 14:29:47 +0100 Message-ID: To: Claude Pache Cc: Rowan Collins , PHP Internals Content-Type: multipart/alternative; boundary="00000000000033fc4f0585136fd8" Subject: Re: [PHP-DEV] [RFC] Permit trailing whitespace in numeric strings From: benjamin.morel@gmail.com (Benjamin Morel) --00000000000033fc4f0585136fd8 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > It is a well-known feature that you can work with such data without previously converting it explicitly to some number type. I'm not requesting to change this: if you're doing string("3.5") + int(7), you should still get float(10.5). What I'm suggesting we change, is comparison of numbers and decimal strings. Thinking about this a bit more, what about following JavaScript semantics? I find them really good in this respect: - when comparing two strings, always use strcmp(): var_dump("42" =3D=3D "42.0"); // false var_dump("3" < "23"); // false - when comparing a number with a string, cast the string to a number, then compare: var_dump(3 < "23"); // true var_dump("3" < 23); // true var_dump(42 =3D=3D "42"); // true var_dump(42.0 =3D=3D "42"); // true var_dump(42.5 =3D=3D "42.5"); // true var_dump(42 =3D=3D "42.0"); // true - if the string does not represent a valid number, always return false: var_dump(42 =3D=3D "42x"); // false var_dump(42 =3D=3D "x42"); // false var_dump(1 >=3D "abc"); // false var_dump(1 < "abc"); // false Note: JavaScript actually allows leading & trailing whitespace around the number: var_dump(42 =3D=3D " 42 "); // true On this last point, I'm not sure whether we should allow both leading & trailing whitespace, or disallow both of them. Other than that, I love the way they handle implicit conversions here. It's so much more understandable, easy to remember, and less prone to errors IMO= . Ben On Wed, 27 Mar 2019 at 11:28, Claude Pache wrote: > > > > Le 27 mars 2019 =C3=A0 00:09, Benjamin Morel = a > =C3=A9crit : > > > > - I would also strongly suggest that 2 strings are always compared > > byte-by-byte; IMO it does (*somehow*) make sense that 42 =3D=3D "42.0",= but > it > > DOES NOT make sense that "42" =3D=3D "42.0". (...) > > There are many ways to obtain numeric data in the form of a string from > external sources (HTML form, database, CSV, ...) and even internal source= s > (bcmath, ...)=E2=80=94and note that, even when such numerical data may ha= ve a > decimal part, it does *not* mean that they are imprecise. It is a > well-known feature that you can work with such data without previously > converting it explicitly to some number type. That works pretty well in P= HP > (well, except that you get intermittent obscure bugs when some third-part= y > code in the same thread plays with setlocale(), but that's another > story...) If we cease to compare numeric strings numerically, I expect th= at > much code will begin to break more or less subtly, and will need to be > thoroughly reviewed in order to be corrected. We must not change that. > > The core issue is that =E2=80=9C=3D=3D=E2=80=9D is fundamentally ambiguou= s: should it compare > as number, as string, as reference, ...? If we were to redesign PHP from > scratch, we may decide other semantics than the current one (e.g., use > distinct operators for comparing numerically and textually, as does Perl)= . > But where we are now, the best thing to do is to follow the common wisdom= : > If you want to compare strings as strings, just use =E2=80=9C=3D=3D=3D=E2= =80=9D. Or strcmp. > > =E2=80=94Claude > > --00000000000033fc4f0585136fd8--