PHP Parse error: syntax error, unexpecteUncaught TypeError: fwrite(): Argument #1…
- User
PHP Parse error: syntax error, unexpecteUncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, bool given
- Assistant
You are encountering two distinct errors:
- PHP Parse error: syntax error, unexpected ...
- Uncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, bool given
Let's address each one separately.
1. PHP Parse error: syntax error, unexpected ...
This error indicates there is a syntax mistake in your PHP code. The error message usually tells you the unexpected token and sometimes the line number, e.g.:
codePHP Parse error: syntax error, unexpected '...' in /path/to/file.php on line 10Common causes:
- Missing semicolon (
;) at the end of a statement. - Missing or extra parentheses, braces (
{},()), or brackets ([]). - Using a keyword incorrectly.
- Unexpected characters or typos.
How to fix:
- Look at the indicated line and lines above it for syntax errors.
- Check for missing semicolons.
- Make sure all opened braces, parentheses, and quotes are properly closed.
- Use an IDE or code editor with PHP syntax highlighting.
- Run
php -l /path/to/file.phpon the command line to get a lint check that can pinpoint syntax errors.
2. Uncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, bool given
This usually means that the first argument you passed to
fwrite()isfalse, but it should be a file resource.Common cause:
You tried to open a file with
fopen(), but it failed, returningfalse. Then, you passed thatfalsevalue tofwrite().Example:
php$fh = fopen('file.txt', 'w'); fwrite($fh, 'Hello World');If fopen fails (for example, file path is invalid or permissions are insufficient),
$fhisfalse, sofwrite(false, 'Hello World')triggers the error.How to fix:
Make sure to check the result of
fopen()before using it.php