In October 2019, Python 3.8 introduced the Walrus operator (:=) via PEP 572 – and thereby triggered one of the biggest controversies in the history of the programming language. The debate was so bitter that Python inventor Guido van Rossum in July 2018 – even before the release – his position as BDFL (Benevolent Dictator For Life). Since the beginning of 2019, Python has instead been governed by an elected Steering Council.
if (n := len(items)) > 10:
print(f"List is too long ({n} elements)")
The governance rift arose immediately following the Walrus discussion—a rare case in which a single language feature had structural consequences. In his statement, van Rossum explained: "The final straw was a very controversial Python Enhancement Proposal. After I accepted it, people took to social media like Twitter and said things that hurt me personally."
He continued: "Now that PEP 572 is over, I never want to have to fight so hard for a PEP again and find that so many people detest my decisions. I want to completely remove myself from the decision-making process." The criticism came not only from the wider community, but also from core developers.
They argued that the operator contradicted fundamental Python principles from the Zen of Python —particularly the preference for simplicity over complexity. After nearly three decades as the undisputed language designer, this marked the end of an era. But while Python had to introduce a new Walrus operator, dividing its community in the process, the question arises: How do other languages handle similar concepts?
The Python operator := (Assignment Expression alias "Walrus") does not exist in PHP – because it is not needed. In PHP, the assignment operator = always Statement and expression at the same time: An assignment returns the assigned value. This is precisely why idiomatic patterns like “assign-and-test” work in if-conditions without their own language feature.
function get_some_field() {
return 'foo';
}
if ($a = get_some_field()) {
echo $a; // foo
}
// Hinweis: $a ist *gesetzt*, selbst wenn die Bedingung falsy wäre.
$a = get_some_field() assigns and evaluates to the right side. If this is “truthy”, you enter the if-Block. Background: In Python, := (PEP 572) because simple assignments are not expressions. In PHP, this has always been different, so there is no equivalent need. Two things often lead to bugs.: = (Assignment) has a lower precedence than most relational operators; parentheses determine the evaluation.
In more complex expressions, you should always use parentheses to increase readability and clarity. ?? has its own, rather low priority. This explains surprises in expressions like $x ?? null === null. Without parentheses, first null === null evaluated. It is better to always explicitly bracket: ($x ?? null) === null. $a also exists outside the if-Blocks – possibly with a falsy value.
function get_some_field() {
return 'bar';
}
if ( ($a = get_some_field()) === 'bar' ) {
echo $a; // bar
}
So it is clear that === 'bar' is applied to the assigned value. Brackets are important here for readability and priority. In practice, this pattern avoids unnecessary duplicate calls (e.g., of the query builder) by storing the query result and then checking it. In productive applications, such as Laravel-based projects, the following pattern can be observed more frequently.:
if ( ($foo = Foo::where('foo', 'bar')) && $foo->count() > 0 ) {
dd($foo->get());
}
&& is short-circuiting: The right part is only evaluated if the left part is truthy. By assigning the statement upstream, you avoid assigning the statement outside the if statement. Whether this is good practice depends on your team's code and analysis tools; semantically, it's correct. At the very least, you should know what the code does if you encounter it in the wild.
PHP doesn't need a "Walrus operator" because assignments are already expressions. This makes idiomatic patterns like "assign-and-test" possible – performant and concise. At the same time, the demands on discipline increase: parentheses, strict comparisons, awareness of truthy/falsy, and clear team conventions are mandatory. Those who follow these guidelines reap the benefits of the Walrus idea without requiring an additional language feature.