Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129581 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 422BD1A00BC for ; Wed, 10 Dec 2025 14:00:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1765375237; bh=nMmiyieKGy2OCtgUEfkDmbuOyVqJkV8ltwDBgLJiZD4=; h=Date:From:To:Subject:In-Reply-To:References:From; b=OZ9JG3i+5tsQgyuBYXRZi/f7zG7H1UfeBLQZQjSyUbtXHsE1PUKyuwYTuZD0vwBin SEh4EVFiUm2C/+tbcMpYFCKf+MWob3Mr5K92TRi00UFgM15lFY8wJDat90gp2f2rDp Vqn3Plwhb8b1nNIbxF1W8p+SaTvQ4CRDDTpNGTIttnF7EpQ4num8IuLH5HbhaIqFpR foAjSvDhEe2pHQadNgZQK9VUuCHtSwqcEzeObaZNrOcvEyXl9625SqAKZmjaUxeNd+ J5BDrqKyBSzSw0nTpyMNdDyUA1XQlIIHD3qOfgo8rYs4WXX+NKJZM2rJMUpHSnh9kr qWNYBaMGxFVqQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E9BBA180077 for ; Wed, 10 Dec 2025 14:00:36 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fout-b6-smtp.messagingengine.com (fout-b6-smtp.messagingengine.com [202.12.124.149]) (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 ; Wed, 10 Dec 2025 14:00:36 +0000 (UTC) Received: from phl-compute-10.internal (phl-compute-10.internal [10.202.2.50]) by mailfout.stl.internal (Postfix) with ESMTP id B2D541D0022E for ; Wed, 10 Dec 2025 09:00:30 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-10.internal (MEProxy); Wed, 10 Dec 2025 09:00:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1765375230; x=1765461630; bh=nMmiyieKGy2OCtgUEfkDmbuOyVqJkV8ltwDBgLJiZD4=; b= LaWdPLOxyqn08OCYDDTW8kueXUnL43hQz1ce2mallGz9UNPuouoCJ9iltaTk3n+Q e2fSU8gbGlulUUsfSU08omFC9E+eqlsIIUc18/DN6kGowE/sNBG0cRVorTIzZR/y TzZJzVedhqez+AubXRf79P9tq5RBfRgaZJqMfUwbwPaAwr6s8rcf/a+YboOHaeuE gt+h67Ai41mGEZKbTPK3uShZNVTuDoHQ7Z1xWolP7Ki53KshwbZcDruglkjYDH/z uR6B5bvU25YKU8EwLADjqNTXRq36XiZv3jvJg0rQGb729U0icJrZ3rD9uCLhV3/S tI/sHkqEVLn14E0PwajPvw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1765375230; x=1765461630; bh=n MmiyieKGy2OCtgUEfkDmbuOyVqJkV8ltwDBgLJiZD4=; b=ImFE5cwKncVE2UPsH OdojhSqJ0E1XW2C+KCcdhrafN5tUpYKjLLqgSkYeKflqbXKIgdfuqOzlNo4ANHBG MQwd7V4HYikgydFKaUougdLB0wYoBCCZIxU/gYujbxxZtjGOGx9Wh7UJELWBlfdQ GxIs4pc47pCHDZxKkoEVIvQ3rbr7T0E/mivv0cHHfpExEswYWKMGQSyePBftGFGq IkmKQWdlOD3m9IIb6C2h2D4eb5KFJGy0uzMU+10SvGN9UPzO2UEf+fAknXuE74Nb 5CiEV6QNzf2RinTyOMzVJwBHNEET58WZk3D4UWjOkbmyw+FHrnib+aiwk08q6OkP ORkhg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddvvdeivdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvufgfjghfkfggtgfgsehtqhhmtddtreejnecuhfhrohhmpedftfhofigrnhcu vfhomhhmihhnshculgfkoffuohfrngdfuceoihhmshhophdrphhhphesrhifvggtrdgtoh druhhkqeenucggtffrrghtthgvrhhnpeelvdeuhedtheduudelfefhgfejhfffvdeljeef gfeuiefgiefgvdehhfefuedvvdenucffohhmrghinhepphhhphdrnhgvthenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprdhphhhp sehrfigvtgdrtghordhukhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouh htpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Wed, 10 Dec 2025 09:00:29 -0500 (EST) Date: Wed, 10 Dec 2025 14:00:27 +0000 To: PHP internals Subject: Re: [PHP-DEV] coerce() function User-Agent: K-9 Mail for Android In-Reply-To: <462A7CF0-71A6-4BFA-B5FE-CDF3E0CCA5E6@gmail.com> References: <462A7CF0-71A6-4BFA-B5FE-CDF3E0CCA5E6@gmail.com> Message-ID: Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") Hi Dmitry, On 10 December 2025 07:42:27 GMT, Dmitry Derepko wro= te: >I suggest to add such function to PHP=2E 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 over= loading function=2E Or range arguments could be replaced with `range(0, 10)= ` function=2E My first thought was that the min/max variant would be covered by the rece= ntly-accepted clamp() function: https://wiki=2Ephp=2Enet/rfc/clamp_v2 Looki= ng closer, I see that you want to default to a specific value, not the near= est, so it's not quite the same=2E I think combining both tasks into one function is definitely the wrong app= roach=2E PHP doesn't have function overloading, and although you can partia= lly stimulate it with nullable and optional parameters, the whole thing end= s up very messy compared to just having separate functions=2E=20 Using range() to pass in an array would be less efficient than min and max= , but maybe good enough for small ranges=2E Perhaps in future we will have = a separate "lazy range" type, which could then be handled as a special case= =2E 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=2E Thanks, Rowan Tommins [IMSoP]