Understanding Fatal error: Cannot use temporary expression in write context
PHP errors can often be misleading if you are not familiar with the specific nuances of how variables are scoped within different control structures. The fatal error Cannot use temporary expression in write context is a classic trap for developers transitioning into PHP 8 or working on codebases that haven't been updated to reflect new syntax rules.
The root cause of this specific error lies in the difference between variable declaration and assignment, particularly when using short array syntax within control blocks like if statements. In your provided example, the line triggering the crash is:
response['error'] = "Error: B is less that C <br />";
At a glance, this looks identical to the correct line above it:
$response['error'] = "Error: A is not equal to B <br />";
The only difference is the missing dollar sign ($) at the beginning of response. However, PHP's parser behaves differently depending on how it interprets the variable name based on its context. When you write $response, PHP treats this as a valid variable assignment. But when you omit the dollar sign and just write response, PHP attempts to interpret this as an array shortening syntax or a property access, rather than a standalone variable.
In previous versions of PHP, writing response['error'] would simply result in a notice about using an undefined variable named response. However, under modern PHP 8 semantics (and specifically within certain contexts like the elseif block), the engine tries to resolve response as a temporary expression or a property access. Since there is no valid way to assign a value to a "temporary" reference without a declared variable, the engine throws a fatal error immediately upon parsing the line, preventing execution from even reaching the assignment logic.
To understand why this happens, consider how PHP parses response['error']. Without the $, the parser looks for a property access on an object or a function call. It cannot find a class context, so it fails to interpret it as a valid write target. The error message explicitly states "write context," indicating that PHP is looking for a variable to store the string but finds nothing to attach it to.
Here is a corrected version of your code snippet where the scope and syntax are consistent:
<?php
$response = [];
$a = 4;
$b = 8;
$c = 9;
$d = 29;
if($a !== $b){
$response['error'] = "Error: A is not equal to B <br />";
}elseif($b < $c){
$response['error'] = "Error: B is less that C <br />"; // Added $ here
}
if($d > $c){
$response['success'] = "Success: D is greater than C<br />"; // Added $ here
}
echo $response['error'];
echo $response['success'];
The fix is straightforward: ensure that every variable assignment starts with a $. This includes variables inside if, elseif, and while blocks. It is a common mental slip to assume that because the variable was defined at the top of the script, you can reference it without the prefix inside specific control structures. This assumption holds for reading values (e.g., echo response['error'] would still trigger a notice or error depending on context), but assignment operators are strict about requiring a valid variable identifier.
One potential pitfall in your original logic is that you initialize $response = [] before the conditional blocks. If you had removed this initialization, PHP would have thrown an "Undefined variable" error on the first access anyway. However, initializing it ensures that subsequent writes to keys like 'success' or 'error' work correctly as long as the syntax is right.
If you are building a larger application using a framework like Laravel, you will rarely encounter raw array assignments in controllers or services without proper typing. In modern frameworks, this pattern often shifts toward returning arrays directly from methods or utilizing typed properties in classes to enforce these rules at compile time (or type-checking runtime). For instance, defining the response structure as an object with typed properties would make such syntax errors impossible to write in the first place:
class ApiResponse {
public ?string $error = null;
public ?string $success = null;
}
By adopting stricter typing conventions or simply being vigilant about variable prefixes, you eliminate this class of runtime fatal errors. Always remember that PHP's error messages regarding "temporary expressions" are strict warnings about the inability to find a valid storage location for your data.