Getting “Parse error: syntax error, unexpected T_IF” in PHP?

Check if the preceding line ends with a ;

This is one of the most common PHP parse errors, and it almost always means you forgot a semicolon on the line before the if statement. PHP’s parser doesn’t catch the problem until it hits the if keyword, which is why the error points to the if line rather than the actual missing semicolon.

For example, this will trigger the error:

$x = 5
if ($x > 3) { echo "yes"; }

Add the semicolon after $x = 5 and you’re good. If you’re using a modern IDE or editor with PHP linting, it’ll catch these before you even run the code.