> On Jun 29, 2025, at 18:39, Thomas Nunninger <tho...@nunninger.info> wrote: > > Hi, > >> I’d like to request *RFC karma* to create a new proposal titled "*Fail-Fast >> Assertion Operator (|=>!|)"*. >> *About the Proposal:* >> The idea is to introduce a new *fail_fast_assertion_operator* to PHP that >> provides a concise and readable way to assert that an expression is valid >> and immediately fail otherwise. >> *Example 1:* >> $name = null; >> $name =>! die("Missing name") > This is equivalent to: >> if (!$name) { >> die("Missing name"); >> } >> *Example 2:* >> $username =>! throw new Exception("Missing username"); >> $password =>! throw new Exception("Missing password"); >> is_valid($username, $password) =>! exit("Invalid credentials"); >> This is equivalent to: >> if (!$username) { >> throw new Exception("Missing username"); >> } >> if (!$password) { >> throw new Exception("Missing password"); >> } >> if (!is_valid($username, $password)) { >> exit("Invalid credentials"); >> } >> The goal is to improve readability and reduce boilerplate in fail-fast >> validation scenarios. This is particularly useful in input validation, >> functional pipelines, and early guard clauses. >> I’d like to open the discussion and collaborate with the community to refine >> and assess its value and implementation feasibility. >> Please let me know if anything else is required from my side. > > > That seems to work today (https://3v4l.org/14aPa): > > > try { > $name ?? throw new Exception("variable is null or not defined"); > } catch (Exception $e) { > echo $e->getMessage() . "\n"; > } > > try { > $name = ''; > $name ?: throw new Exception("variable is falsy"); > } catch (Exception $e) { > echo $e->getMessage() . "\n"; > } > > > Regards, > > Thomas
You can also use logical OR (||), similar to other languages: $name || throw new Exception("variable is falsy"); Cheers, Ben
signature.asc
Description: Message signed with OpenPGP