Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126989 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 qa.php.net (Postfix) with ESMTPS id 328481A00BC for ; Mon, 31 Mar 2025 22:00:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1743458311; bh=d4nPvNXP6ONPBHF0osXrCGhZgHlN3Q48pDetgqZUzsg=; h=Date:To:From:Subject:From; b=AOKAOMNv86N6jgPHOJ83e3jpLnnzQTItI/WtzX6443JUqX1TUvhdCBbxX5O5vSDcz LABNMWZwf1eyQTLwL21ShbJi6CmJjoS/XULJYPss6gNwnRgtd8Fg0ZnxpN3pkRvr8v BPj+c6H1b/sbu7LcVGe+ejmB1BrDI/9lD/7YF6/fSLFBZKDC9cK2ZQdPigpD41okon 173KLz2EXsJSBVQeoIG05cMnhRGIZFagmlJ10w/16HSMFoKEgCJJ/iGh0tQUd1fE9e glkLVJ1jrp098sJ0Cnxa1IGuhaHO6HzGCJ/jBF5+JuLYxiKwcaseSsrt1W5p4vHGdF wIHaTbnoXEJ0g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4B8E6180088 for ; Mon, 31 Mar 2025 21:58:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 ; Mon, 31 Mar 2025 21:58:31 +0000 (UTC) Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-43ce71582e9so34500465e9.1 for ; Mon, 31 Mar 2025 15:00:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743458457; x=1744063257; darn=lists.php.net; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=d4nPvNXP6ONPBHF0osXrCGhZgHlN3Q48pDetgqZUzsg=; b=M4m4lLEW4COM4bPE/fOMFVW1hpB+coYi9BqblQMt8+10VYyTMerhfHrVQq7ZkZ9P+V JOzRrSCQ9GVvPdzjAY0B4Bsla4ZpbA3357UaYDFvjFlEltZ78vMdQh4/SLhABMntRHza 5m1150QQ2w59YRbqwJl7R7E9oVtAI9z3PwzJ3U7kuCT1wZJ85DeKZ6gCIGOYbf7dkxmL CW5qiQk2+R/mBmQcRuuWxd6RNiWQmD8D5lc0Ugu1kywO/JMjSV8WRFP1RA64DWTSqMlG OTeWiIfpbDyjnyDbmbDhHOZEEnmysxvy/rJTH5LkxeP7B33gIjijmoU8EH5ohNy/W4K5 6sVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743458457; x=1744063257; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=d4nPvNXP6ONPBHF0osXrCGhZgHlN3Q48pDetgqZUzsg=; b=fY7LdtVxCsxh0GysoKvjgG8dp13YKaUAmiPka0z3oFKNPq3ivDh0fZrETzdBlc/U5G SckWWY+0ohx7zsY0R9y3xg3qHnaYIj4lmEJ8n/GylToaOBvQ00DVqKAoEZRtrI2FqVLv xJBeJwdvmL+kTZFWua+5le1hXJ9I/mIUyoBB3zxuruOrBvpishlFwgGQk7goSgoFCbEO DVI/61PLSF/kuJdKGz0+y+3c6S4L/vFoQgVuaXBjNeLc/5rF821W4ZYCBChJS0ATkGUy TEZCQ3y2W3j0TqoM2Eyciib2h++kLiUSPvimUn+AFFK5HwCXfiS19XXKnjWeMwdBHkBi t29A== X-Gm-Message-State: AOJu0YwX8rtDOF0hYHo+xR0sPUU3Udknlm7WvJLqqvDzuBHqWbV0W/K/ EAQdETUyihwdRlt87sDjtasQ4gWt8ADc/+RczviNMUbMwOjqy0dRQHtouK7k X-Gm-Gg: ASbGncvIwhvk95jdQ+KrDZgWxQzIYgsDexQFFeP/YdZY6RlAwdL7H/k9ZMNtdJtCHxN SUMyVTZpz0ammajspfCPI7Ov8wXdssYze3oFf3vaeQpFHrRSn+ZbyG3uEYr+JTOHMWkcNgI+GHA ZZxTrjIB2qk40JydJ+TzZbgVSpgeHbJQc8JKP/r0g09j6D0PUs2WFg0Sv62aQXVtnR7kZfSCel4 NjkAk6oxaym2dpXU3UdQaMAvezrJiJBlaAg+tz6J/BaIVuyxIi6vMWMjeH7b7PpgzPheWKuQQDf bqEI+lM6NoX9W2ke9aJAoGUG2cdk9eR9M6Oi+mi5v7L+/2lfCnQshJRRykCrJ1dqYX0msHM9Oe3 oQMeobv05HrAk7BoXecoXxxVGUNaT6CeHq/Huz00OWx02umvvYnW/sx1sh6KUEv6stNLo0n2xwW oya5gsRDS0 X-Google-Smtp-Source: AGHT+IHPm7XCWHSVH3ySwraG1XW8SyEkcoeYX0agoQ/JmQA1KK9FuKbhkdvkg5yMwefQJIW2Sd7bPw== X-Received: by 2002:a05:600c:4f82:b0:43d:17f1:2640 with SMTP id 5b1f17b1804b1-43db62b5ba8mr84084065e9.26.1743458456832; Mon, 31 Mar 2025 15:00:56 -0700 (PDT) Received: from ?IPV6:2a02:1811:cd2f:3500:e093:fcad:4f6:e542? (ptr-du5vm7f5ekzptmf0fgi.18120a2.ip6.access.telenet.be. [2a02:1811:cd2f:3500:e093:fcad:4f6:e542]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d900008d8sm132600015e9.33.2025.03.31.15.00.56 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 31 Mar 2025 15:00:56 -0700 (PDT) Message-ID: <4a3c6ce7-102d-4cfe-a7a8-35630715b870@gmail.com> Date: Tue, 1 Apr 2025 00:03:03 +0200 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: PHP internals Subject: [PHP-DEV] [RFC brainstorm] Approximately equals operator Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: dossche.niels@gmail.com (Niels Dossche) Hi internals! I'm excited to share what I've been working on! I had an epiphany. I realized what we truly need to revolutionize PHP: a new operator. Hear me out. We live in an imperfect world, and we often approximate data, but neither `==` nor `===` are ideal comparison operators to deal with these kinds of data. Introducing: the "approximately equal" (or "approx-equal") operator `~=` (to immitate the maths symbol ≃). This combines the power of type coercion with approximating equality. Who cares if things are actually equal, close enough amirite? First of all, if `$a == $b` holds, then `$a ~= $b` obviously. The true power lies where the data is not exactly the same, but "close enough"! Here are some examples: We all had situations where we wanted to compare two floating point numbers and it turns out that due to the non-exact representation, seemingly-equal numbers don't match! Gone are those days because the `~=` operator nicely rounds the numbers for you before comparing them. This also means that the "Fundamental Theorem of Engineering" now holds! i.e. 2.7 ~= 3 and 3.14 ~= 3. Of course also 2.7 ~= 3.14. But this is false obviously: 2 ~= 1. Ever had trouble with users mistyping something? Say no more! "This is a tpyo" ~= "This is a typo". It's typo-resistant! However, if the strings are too different, then they're not approx-equal. For example: "vanilla" ~= "strawberry" gives false. How does this work? * The strings are equal if their levenshtein ratio is <= 50%, so it's adaptive to the length. * If the ratio is > 50%, then the shortest string comes first in the comparison, such that if we ever get a `~<` operator, then "vanilla" ~< "strawberry". There is of course a PoC implementation available at: https://github.com/php/php-src/pull/18214 You can see more examples on GitHub in the tests, here is a copy: ```php // Number compares var_dump(2 ~= 1); // false var_dump(1.4 ~= 1); // true var_dump(-1.4 ~= -1); // true var_dump(-1.5 ~= -1.8); // true var_dump(random_int(1, 1) ~= 1.1); // true // Array compares (just compares the lengths) var_dump([1, 2, 3] ~= [2, 3, 4]); // true var_dump([1, 2, 3] ~= [2, 3, 4, 5]); // false // String / string compares var_dump("This is a tpyo" ~= "This is a typo"); // true var_dump("something" ~= "different"); // false var_dump("Wtf bro" ~= "Wtf sis"); // true // String / different type compares var_dump(-1.5 ~= "-1.a"); // true var_dump(-1.5 ~= "-1.aaaaaaa"); // false var_dump(NULL ~= "blablabla"); // false ``` Note that this does not support all possible Opcache optimizations _yet_, nor does it support the JIT yet. However, there are no real blockers to add support for that. I look forward to hearing you! Have a nice first day of the month ;) Kind regards Niels