Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129591 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 689AE1A00BC for ; Fri, 12 Dec 2025 07:23:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1765524244; bh=FopYJK3LnACKObPzQDk91sU4zbHhScCkPYhhy/ESGo0=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=Lt3fgGoDaj9VrSIi87eKQK6XudCWr1Q1SAtVh9CwBeGRDXPOBqcuOcRQvU0Qz1bw1 cnmd7Fia4FI+as1xjtrQIGph7ux9ZGFps3tFqXqWfxvEyhdgSzh4cKjH8Iv26Je1mS YVvP9YfUyegdO4H5j4Z4AeOWXf6SCD01y40eJ+gjdTiz+BYdw+Na5bAvgu+IDyTy9U fwJSyJLL+R6XhcWIOYaeUMiMe4mMzTpPZfEgzQ9kat+iPw23wr6eBM9e4RXipADWPJ AXnxw+PI2r8WyM6fMIU43OLRNkmu130NdERFQGXi42WSGBdB909ye7SdeVOuxDy16S m8WestbBaGC7w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2C33F1801C7 for ; Fri, 12 Dec 2025 07:24:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, T_SPF_TEMPERROR autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 12 Dec 2025 07:23:59 +0000 (UTC) Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-42b3c965cc4so471428f8f.0 for ; Thu, 11 Dec 2025 23:23:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765524234; x=1766129034; darn=lists.php.net; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=cjiHpzN1nmzjmmmx2am7ZTzUJqXlvkcZMf63wKju6oU=; b=Up03+qqvyIFKpxPGCXT8csfbOLqMk8ZqMlOvBMKhzzG7pDCrhTXPr1YC/20AA8Juip N2x/ltJvxPqdduE1LXtOLnXlucd8cqDpqW5+i2yzpD/lUS1FlgMC1r6bTMegFF88H+x7 rGqAJhhsJXVxzQJIgR3fwykkg+I2NMu8pMVLgqB7G8xv5g34gortBGJjZB6irR8okvxb n2kp/JfSTWftWbDfL/8/5BPa5PY0oY6Mi2F6RPYA1EUgYujRmvnd+qmdrT3Y8mIYT5ab 6nNd6oUklg5K5Z++rCCVQbCVvvB1QYHqiQ0wbglSNpJ137p3ua1PHTyYxWiKHPiN4ZSl CuZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765524234; x=1766129034; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=cjiHpzN1nmzjmmmx2am7ZTzUJqXlvkcZMf63wKju6oU=; b=pQF5s/3i+O26i1NecqPojCkuDmz8t+4ovodEWK0di2QrUHOZMSgXCM7B/S5xrd8FPz OPPe6Fk7bdVB8ZQFSxiC7250XPOf3koQx6r8YslNgXczCGLkaDdOuYdwKez5l8+yReK/ euI6W4BRa4PVnjQZBLiysnhbdt6BI0QhzqJdbydttbZzk0kKPQECPQrRyjEiPbjzLAvm 4hTjG/Z5I0DKlvlqqZuAB6Cjz6XeTYUEQAjoTQRnRxPY52GJdXLA70pUgFyiePcY+ic4 THTKmfN1nB8cSBD/yzWUS4viPvj8sNqxXbpP9Q9kRdTxhRGvnBjMTvgxw+Fy8ZYdgT0j FL1g== X-Gm-Message-State: AOJu0YwnHEbZ5GygPPzOOSAtCfa360lwGP6dF2/OdpuLpio4L0cOoJiH R7WU/At5Tkoh1KqNXWMVSnRw/w89779NHDwsdNiucwz/rf4pSlDW550V5XlwPT3L X-Gm-Gg: AY/fxX6XKDLNz1OJHKS6/kUBYRsAnJtbCk/6EgGOisyFKyWCh7j7yZCq+hUwdmf2W1f 0vri+stTT6hkBDX8mK2cHXKNszY4A+EV1yEzj3lYEsLe7qxiHsmatDgo4mKl+ktsuvVX2GavZza j79Bkl4sUtqCv88CBxZkxpJ88uuB7gfljwju9cn+JsnylGNnCQj/27JLuerZbx6jrAH+QI8lUaz RVMDTANrDZ1Wuksp87j+a0dL3MOhvhsMca1mvfC3uX2PHt8yrf4g0s5SBTe9XbPP9TJtGAXvlmf clSDvdXZVamwo9Ig/VpsPRLfwbSs5ClqaceTleWinubWeYN6EkM+LnGS5lbmKMXKkMPCbFg3Y2A GLrGy1brpMUo747k4iveWrTdkA+kL8pvHn/fQK2RNdKpPu7h2iCzSALlUtje4ejkbj9OTJY5LGC KSyAITC/3utCfX8zZZEusbGhOWSGGTOJcgVqs= X-Google-Smtp-Source: AGHT+IEwZw1USAbdH95wscjZ0qumF/rWHTXY4i9HONlDWKBQDazON4Wy7SOiJ2R1TdNPT4ib3ovgXA== X-Received: by 2002:a05:6000:144a:b0:429:bfbb:5dae with SMTP id ffacd0b85a97d-42fab263721mr6039288f8f.17.1765524233486; Thu, 11 Dec 2025 23:23:53 -0800 (PST) Received: from smtpclient.apple ([178.134.17.116]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-42fa8a66614sm10217387f8f.5.2025.12.11.23.23.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Dec 2025 23:23:53 -0800 (PST) X-Google-Original-From: Dmitry Derepko Message-ID: <232310F3-47A1-4F55-B225-9B4159ACA39E@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_9502A087-FE12-4236-91A4-FD43FE3D8457" Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81.1.3\)) Subject: Re: [PHP-DEV] coerce() function Date: Fri, 12 Dec 2025 11:23:40 +0400 In-Reply-To: Cc: PHP internals To: "Rowan Tommins [IMSoP]" References: <462A7CF0-71A6-4BFA-B5FE-CDF3E0CCA5E6@gmail.com> X-Mailer: Apple Mail (2.3826.700.81.1.3) From: xepozzd@gmail.com (Dmitry Derepko) --Apple-Mail=_9502A087-FE12-4236-91A4-FD43FE3D8457 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Dec 10, 2025, at 6:00=E2=80=AFPM, Rowan Tommins [IMSoP] = wrote: >=20 > Hi Dmitry, >=20 > On 10 December 2025 07:42:27 GMT, Dmitry Derepko = wrote: >=20 >> I suggest to add such function to PHP. The examples above may look = like the following >>=20 >> $var =3D coerce($input, variants: [=E2=80=9Cyes=E2=80=9D, =E2=80=9Cno=E2= =80=9D], default: =E2=80=9Cyes=E2=80=9D); >> $var =3D coerce($input, min: 0, max: 10, default: 5); >> $var =3D coerce($input, min: 0, max: 10, default: 5); >>=20 >> For sure, that could be different functions not to introduce another = overloading function. Or range arguments could be replaced with = `range(0, 10)` function. >=20 > My first thought was that the min/max variant would be covered by the = recently-accepted clamp() function: https://wiki.php.net/rfc/clamp_v2 = Looking closer, I see that you want to default to a specific value, not = the nearest, so it's not quite the same. >=20 > I think combining both tasks into one function is definitely the wrong = approach. PHP doesn't have function overloading, and although you can = partially stimulate it with nullable and optional parameters, the whole = thing ends up very messy compared to just having separate functions.=20 >=20 > Using range() to pass in an array would be less efficient than min and = max, but maybe good enough for small ranges. Perhaps in future we will = have a separate "lazy range" type, which could then be handled as a = special case. >=20 > In general, I don't think it's a function that I'd use often, but it = seems a reasonable enough addition to the language. >=20 > Thanks, >=20 > Rowan Tommins > [IMSoP] Thanks for your thoughts. I agree that it=E2=80=99s better to have a few separate functions for = different use cases. Regarding the lazy ranges, basically, it=E2=80=99s kind of `iterable`, = but with bounds.=20 I think it could be done as the others are: new IntRangeIterator(start: = 0, end: 10, inclusive: true) / or as a new function range_iterator / or = as a new flag argument in the original range function. So, there are could be a function with: - manual range bounds =E2=80=94 strings =E2=80=94 ints =E2=80=94 enums? - iterator range bounds =E2=80=94 IntRangeIterator(start: 0, end: 10, inclusive: true) =E2=80=94 CharRangeIterator(start: =E2=80=98A', end: =E2=80=98D', = inclusive: true) =E2=80=94 Any other `iterable`, including Generators and regular arrays Actually, looks quite reasonable to re-use iterables. Nice catch, I = really like it. ---------- Best regards, Dmitrii Derepko. @xepozz --Apple-Mail=_9502A087-FE12-4236-91A4-FD43FE3D8457 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On Dec 10, = 2025, at 6:00=E2=80=AFPM, Rowan Tommins [IMSoP] = <imsop.php@rwec.co.uk> wrote:

Hi Dmitry,

On 10 = December 2025 07:42:27 GMT, Dmitry Derepko <xepozzd@gmail.com> = wrote:

I suggest to add such function = to PHP. The examples above may look like the following

$var =3D = coerce($input, variants: [=E2=80=9Cyes=E2=80=9D, =E2=80=9Cno=E2=80=9D], = default: =E2=80=9Cyes=E2=80=9D);
$var =3D coerce($input, min: 0, max: = 10, default: 5);
$var =3D coerce($input, min: 0, max: 10, default: = 5);

For sure, that could be different functions not to introduce = another overloading function. Or range arguments could be replaced with = `range(0, 10)` function.

My first thought was that = the min/max variant would be covered by the recently-accepted clamp() = function: https://wiki.php.net/rfc/clamp_v2 Looking closer, I see that = you want to default to a specific value, not the nearest, so it's not = quite the same.

I think combining both tasks into one function is = definitely the wrong approach. PHP doesn't have function overloading, = and although you can partially stimulate it with nullable and optional = parameters, the whole thing ends up very messy compared to just having = separate functions.

Using range() to pass in an array would be = less efficient than min and max, but maybe good enough for small ranges. = Perhaps in future we will have a separate "lazy range" type, which could = then be handled as a special case.

In general, I don't think it's = a function that I'd use often, but it seems a reasonable enough addition = to the language.

Thanks,

Rowan = Tommins
[IMSoP]


Thanks for your thoughts.

I agree that = it=E2=80=99s better to have a few separate functions for different use = cases.

Regarding the lazy ranges, basically, = it=E2=80=99s kind of `iterable`, but with bounds. 
I = think it could be done as the others are: new IntRangeIterator(start: 0, = end: 10, inclusive: true) / or as a new function range_iterator / or as = a new flag argument in the original range function.
So, there = are could be a function with:
- manual range = bounds
=E2=80=94 strings
=E2=80=94 = ints
=E2=80=94 enums?
- iterator range = bounds
=E2=80=94 IntRangeIterator(start: 0, end: 10, = inclusive: true)
=E2=80=94 CharRangeIterator(start: =E2=80=98A',= end: =E2=80=98D', inclusive: true)
=E2=80=94 Any other = `iterable`, including Generators and regular = arrays

Actually, looks quite reasonable to = re-use iterables. Nice catch, I really like it.

----------

Best = regards,
Dmitrii = Derepko.
@xepozz

= --Apple-Mail=_9502A087-FE12-4236-91A4-FD43FE3D8457--