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

» Using Cookies to Save Data in PHP

Cookies are a useful way to save information, for example a user preference or login session locally on the client's machine. PHP makes creating and accessing cookies very easy. To create a cookie called myCookie with the value hello,world, simply add the following line of PHP code:$expireTime = 60 * 60 * 24 + (); // 1 day("myCookie", "hello,world", $expireTime); This data will then be available the next time the user loads that particular page. To access the cookie at a later date, use the...

» For Loops in PHP

To create a for loop in php, follow these simple steps:Set a counter variable to some initial value.Check to see if the conditional statement is true.Execute the code within the loop.Increment a counter at the end of each iteration through the loop.The following is pseudo php code for a for loop:123for(init counter; conditional statement; increment counter){ inner code;}As you can see, all of the above steps are taken care of within the for loop, making the syntax short and easy to...

» Access restriction on class due to restriction on required library

If you are using Eclipse or Rational Application Developer (RAD) and encounter the following error causing your build to break, here is a work around that will help you get past the problem:Error: "Access Restriction: The type {class name} is not accessible due to restriction on required library: {library path}"Work around: In Eclipse or RAD, go to Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (Access...