Parse error: syntax error, unexpected 'else' (T_ELSE)
When PHP throws a Parse error: syntax error, unexpected 'else' (T_ELSE) on line 235, it is screaming that the parser cannot find the matching opening brace { for your if statement before encountering the else. This is not a runtime logic error; the script fails to load entirely because the code structure is broken at the lexical level.
Looking closely at the provided snippet, the root cause is an indentation-based illusion. The while loop and its body are indented, but crucially, the closing brace for that if block is missing or misplaced relative to the logic flow. In your specific code, you have an if ($rows > 0) block. Inside it, there is a while loop. However, the closing brace } that should terminate the if statement appears after the else keyword in the logical structure of the error message, or rather, the parser sees the else as belonging to the if, but the indentation suggests the if block might be closed prematurely or the braces are mismatched.
Actually, looking strictly at the code provided:
if ($rows > 0){
while (mysqli_stmt_fetch($stmt)) {
// ... logic
echo "...</tr>";
}
} else { // Line 235 is here
echo "...";
}
} // This closing brace at the very end has no matching if/while
Wait, let's re-examine the specific indentation in your snippet. The code shows:
if ($rows > 0){
while (mysqli_stmt_fetch($stmt)) {
// ...
}
} else {
// ...
}
} // This last brace is the problem if it exists, or missing braces inside.
The error says unexpected 'else'. This happens when PHP expects a statement but finds else instead. The most common scenario for this specific error pattern in your code is that you have an opening brace { immediately after the if condition, and then you close that block with a } before the else.
In your snippet:
if ($rows > 0){
while (...) { ... }
} else {
This structure is actually syntactically valid in modern PHP. However, if you look at the very end of the provided code block:
}
}
}
There are multiple closing braces. Let's trace the nesting.
if ($rows > 0) {opens a block.- Inside,
while (...) {opens a loop. - The loop closes with
}. - Then we hit
}which closes theif. - Then we hit
else {.
If the code actually looks like this:
if ($rows > 0){
while (mysqli_stmt_fetch($stmt)) {
// ...
} // Loop ends here
} else {
// ...
} // This brace is unexpected if it's meant to close the outer scope or another missing block
}
The specific error unexpected 'else' usually implies that there is a closing brace } that closes the if block before the else keyword. PHP treats an if (...) { ... } else { ... } as a single unit. If you write:
if ($condition) {
// code
}
else {
// code
}
This is valid. But if you wrote:
if ($condition) {
// code
}
} else {
// error here
}
The parser sees the first } as closing the if. It then expects a new statement (like another if, while, or variable assignment). Instead, it finds }, which closes nothing (syntax error), and then else. Or more likely, you have an extra } somewhere.
However, the most probable cause for "unexpected 'else'" in this context is that the while loop or the if statement was closed incorrectly before the else. Look at this common mistake:
if ($rows > 0) {
while (...) {
// code
}
// Missing closing brace for IF? Or extra one?
} else {
If your code actually has an extra closing brace right before the else, like this:
if ($rows > 0){
while (...) { ... }
}
} else {
// The first } closes the if. The second } is unexpected. Then else follows the stray brace.
But looking at your source, the line numbers suggest a specific indentation issue. Often, developers copy-paste code where the closing brace of the if is indented under the loop, and then they try to put an else at the same level. If the indentation implies that the } closes the if, but the else is intended for a different scope, it breaks.
Actually, there is a simpler explanation often missed: The else belongs to the if, but the if block was closed prematurely.
In your code:
if ($rows > 0){
while (mysqli_stmt_fetch($stmt)) {
// ...
}
} else {
This is valid. But look at the end of your provided snippet:
}
}
}
It seems you have three closing braces at the bottom.
- One closes the
while. - One closes the
if. - One closes... what?
If the structure is:
if ($rows > 0) {
while (...) { ... }
} else {
// content
}
} // This third brace is the culprit if it exists.
Or, perhaps the while loop was not closed properly before the else.
The fix is straightforward: Ensure there is exactly one closing brace for the if statement immediately preceding the else, and no stray braces. The structure must be:
if ($rows > 0) {
while (mysqli_stmt_fetch($stmt)) {
// calculations
echo "...";
}
} else {
echo "Not rows";
}
If you have additional code outside this block, close it with a new brace. The error occurs because PHP sees the else as a continuation of the previous block, but the previous block was already terminated by a } that didn't belong there, or the indentation misled you into thinking the braces were balanced when they weren't.
Additionally, your code mixes prepared statements with direct string interpolation (WHERE username = '$username'). While $stmt is created via prepare, the string contains single quotes around variables. If $username comes from a user input, this re-introduces SQL injection risks if not handled correctly, though technically valid in PHP if variables are passed by reference or properly escaped before interpolation (which they aren't here). For safety, use placeholders:
$stmt = $mysqli->prepare("SELECT date, impressions, balance FROM reports WHERE username = ? AND date BETWEEN ? AND ?");
$stmt->bind_param("sss", $username, $firstDay, $lastDay); // Note: 's' for string, 'i' for int depending on types
Finally, regarding the logic: You calculate $ecpm3 by dividing by $impressions. If $impressions is 0 (which is possible in reports), this will generate a Division by zero warning or result in INF. You should add a check: if ($impressions > 0) { ... } else { $rate = 0; }.
To resolve the specific syntax error, verify that there are no extra closing braces } before the else keyword and that the indentation reflects the actual nesting depth. Remove any stray braces and ensure the if-else pair forms a single continuous block. If you are using an IDE or linter, enable strict error reporting (error_reporting(E_ALL);) to catch these structural mismatches during development rather than on line 235 in production.