PHP Parse Error: Unexpected $

(0 votes, average 0 out of 5)

Error

A PHP parse error returns, citing an "unexpected $".  Because this is a parse error, the PHP script does not run to any extent.

Solution

This error almost always results in a missing bracket somewhere.  Check your code carefully for missing brackets.  Using a PHP IDE can be useful for checking brackets that are not closed.  Brackets that span multiple PHP sections can be the trickiest to miss.  For instance:

1
2
3
4
5
6
7
8
9
10
<?php
$x = 3;
if($x == 3){
?>
 
<a href="http://www.thetechrepo.com">The Tech Repo rules!</a>
 
<?php
}	//CLOSING BRACKET -- EASY TO MISS
?>

 


It can be easy to forget the trailing bracket when html is placed in between two PHP brackets.  Fix the bracket problem, and this parse error should go away.

Partner Links:
Last Updated on Monday, 19 July 2010 13:08  
Related Articles

» PHP - Check if a string contains a substring

In many object-oriented languages, you often want to check for the presence of a substring in a string. PHP does not contain a .contains() method which you can use, but you can create your own using the function quite easily:12345678910111213141516171819/*** Checks to see of a string contains a particular substring* @param $substring the substring to match* @param $string the string to search * @return true if $substring is found in $string, false otherwise*/function contains($substring,...

» Understanding File and Folder Permissions (POSIX) on Linux and Mac OS X (using chmod)

Linux and Mac OS X both use a -compatible type of filesystem. This has to do with the way files are accessed by their owner and others. Files and folders have permissions for 3 different types of users:Owner: this is the user who owns the file or folderGroup: this is the permissions for any user in the group that the file belongs toOthers: this is the permissions for all other users who don't fall into one of the two above categoriesFor each of these types of users, there are 3 different...

» Resolve Input/Output error when trying to write to WebDAV (davfs2) mount like box.net on Linux

These instructions assume that you already configured the WebDAV filesystem by following .If you are mounting a WebDAV folder on Linux using the davfs2 FUSE filesystem, you may encounter the following error when trying to write a file to the WebDAV filesystem:cp: cannot create regular file `test.txt': Input/output errorThis is caused by the WebDAV filesystem not supporting file locks. The solution is to disable file locks in the davfs2 configuration file, located...