LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   What's the meaning of "PHP Parse error: parse error, unexpected $ in..." (http://www.linuxquestions.org/questions/programming-9/whats-the-meaning-of-php-parse-error-parse-error-unexpected-%24-in-433706/)

frandalla 04-10-2006 11:10 AM

What's the meaning of "PHP Parse error: parse error, unexpected $ in..."
 
I've being working on a project and out of the blue I've being getting this error though it wasn't here a while before.

What's the exact meaning of this error?

PHP Parse error: parse error, unexpected $ in main.php on line 59

taylor_venable 04-10-2006 11:33 AM

Line 59 has a $ where there shouldn't be one. In other words, it doesn't start a variable name. Common causes may be lack of semicolon to end previous line, or invalid variable name, or unescaped end-of-input anchor for a regular expression. If you can't locate it, go ahead and post that area of your code and I'll be happy to take a look at it.

Hko 04-10-2006 11:36 AM

That means there was a '$' in the code in line 59 which should be there according to PHP's syntax.

For us to be of some help to you, please post the code or, if it's large, at least line 59 of file "main.php"...

graemef 04-10-2006 12:07 PM

As HKO suggested please post the code or at least the appropriate lines say 50 - 70 (or the function boundary if that make sense.

frandalla 04-10-2006 01:15 PM

The point is: line 59 is the LAST line of the PHP code. Nothing last. It ends with a "}" on line 58 and 59 is a blank line.
I gotta leave very fast now. As soon as I get back I can post the whole code for you guys! :) thanks for the answers.

My guess: I think that $ means "end of line" (as in VI) and not a stray "$".
try to be back ASAP

paulsm4 04-10-2006 01:24 PM

I disagree - I think the error means just what is says: that PHP found a "$" where there shouldn't be.

Before you post anything, please:
1. Comment out your PHP (to see if the problem goes away - it should!)
2. Put in some debug "echo" statements to see how far you get before the problem recurs

I believe you've probably got a bona fide syntax error in your PHP code, and I believe "divide and conquer" might be the best strategy to isolate and correct it.

IMHO .. PSM

frandalla 04-10-2006 10:20 PM

I'm posting the code here for you.
I'm using php 5.0.5
Apache 2.0.55
I'm pretty sure there's no "$" hanging lost inside my code, I'm not such a beginner programmer.
Why am I guessing that?
This code used to work in my old linux installation. Now all files present me this very same error. I really don't know what to do.
PHP Code:

<?php
session_start
();
include(
"file.php");
$query="select * from user where user_name = '".$_POST['user']."';";
$link mysql_query($query);
$line mysql_fetch_array($link);
if((isset(
$_SESSION['pass']))||($line['user_pass'] == sha1($_POST['pass'])))
{
if(!isset(
$_SESSION['pass']))
{
$_SESSION['user'] = $_POST['user'];
$_SESSION['pass'] = $_POST['pass'];
}
?>
<html>
<head>
<script src="drop_down.js"> </script>
<style type="text/css">
@import "style.css";
</style>
</head>
<body  >
<div style="position:absolute;left:0%;top:2%">
<img src="images/webcontabil_small.png" alt="Logo" width="139" height="169" border="0">
<br><br><br>
<?//include("menu.php");?>
</div>
<?
if(!isset($_SESSION['emp_id']))
{
?>
<script language="javascript">
document.location.href="emp_sel.php";
</script>
</body>
</html>
<?
}
}
else
{
?>
<html>
<head>
<style type="text/css">
@import "style.css";
</style>
</head>
<body  >
<div style="position:absolute;left:50%;top:50%;margin-left:-100px;margin-top:-180px;" class="f">
<IMG src="images/webcontabil_small.png" alt="logo" width="126" height="171" border="0" style="margin-left:25px;"><br><br>
Senha Incorreta. Tente novamente.
</div>
</body>
</html>
<?
}
?>


paulsm4 04-10-2006 10:27 PM

Quote:

Before you post anything, please:
1. Comment out your PHP (to see if the problem goes away - it should!)
2. Put in some debug "echo" statements to see how far you get before the problem recurs

I believe you've probably got a bona fide syntax error in your PHP code, and I believe "divide and conquer" might be the best strategy to isolate and correct it.
I'd add to the list:
Quote:

3. Change your '<?' tags to '<?php'
4. Check your php.ini and see what happens if you enable "register globals"
My guess is that you're having problems with the older dialect of PHP on your new PHP 5.0 system.

Again: please modify the syntax and try the "divide and conqueror" approach (steps 2 and 2).

And please let us know what happens!

'Hope that helps .. PSM

frandalla 04-10-2006 10:35 PM

My old installation was a 5.* PHP system.
I'll just try to rewrite the whole file and see what I can get. I have more than 10 files returning me the same error. Which was not here before. I wonder...
Any help will still be appreciated. Thanks to all who answered me. As soon as I have news I'll keep you up to date.
Quote:

Originally Posted by paulsm4
I'd add to the list:

My guess is that you're having problems with the older dialect of PHP on your new PHP 5.0 system.

Again: please modify the syntax and try the "divide and conqueror" approach (steps 2 and 2).

And please let us know what happens!

'Hope that helps .. PSM


graemef 04-10-2006 10:38 PM

Thoughts...

Double check that it is this file that is being read by php and not another file with the same name in a different directory (that sort of thing)
change <? to <?php
Add a liberal dose of echo statements so you know where you are

As an aside your code would appear to be open to a SQL injection attack

frandalla 04-11-2006 08:35 AM

Guys, this error don't get me even to start running the file! It just gives the error. It's most likely what Paulsm4 said. Something that my old install of php recognized as OK and can't do it now. strange indeed is, as the system was a 10.2 slackware system too. As I told you, I'll try rewriting the whole file. I'll keep you informed.

Spudley 04-11-2006 08:56 AM

When you get this error on the last line of your PHP, it usually means that you left something unclosed -- ie mismatched brackets, php tags, or something like that. PHP freaks out because it gets to the end of the program and is expecting more code; it's reporting "unexpected $" because it treats the blank line after the final ?> as a print statement (ie print a carriage return).

As you say, it works in PHP4, and I don't have php5 on my box to test it with. Looking at the code, I can't see anything obviously wrong, but the line <?//include("menu.php");?> makes me wonder if php5 is treating the comment differently, and including the ?> in the comment, in which case, it would definitely cause problems. So maybe try changing that to use /*...*/ comment style instead.

I honestly can't see anything else that would cause problems. Hope you manage to solve it... If all else fails, split it down into bits, and see which one breaks. :)

All the best.

graemef 04-11-2006 11:13 AM

Quote:

Originally Posted by frandalla
Guys, this error don't get me even to start running the file! It just gives the error.

Okay, just remove the contents of the file until it does work. Start with:

PHP Code:

<?php
echo "Hello";
?>

Just so you know it is using that file. Does that work?

Code:

if yes then
start adding lines of code maybe start with the first php block, that's about 15 lines.

Code:

else // it still didn't work
That probably means that it's not reading your file but another, just to be sure put an html only file in the same directory and try that.

graeme.

taylor_venable 04-11-2006 12:54 PM

It could be a problem in your included file (from the include() directive near the top) that's flowing over into this file. For example, a mis-matched parenthesis or bracket that's causing the parser to fail at the end of the whole input.

paulsm4 04-11-2006 01:29 PM

frandalla - please do what graemef is suggesting. He's given a very good example of what I was calling "divide and conquer"; he's making the excellent point that maybe the failure isn't even occurring on the file you think it is. Graemef's advice also ties in well with taylor_venable's and spudley's equally well-considered suggestions.

Please try it and let us know what happens.


All times are GMT -5. The time now is 08:57 PM.