Page 1 of 1

PHP Parse error: syntax error, unexpected T_LNUMBER Error

Posted: Sun Jun 04, 2006 4:58 pm
by tbrown
Hello my name is Tom. I am new to PHP. I bought the "PHP5, Apache, MYSQL" book by Liz Naramore(and others). I have been doing well - but hit a snag. I am copying vebatim from the book (with the exception of using " instead of "HEREDOC" to close a group of code). This is the error I get:

Parse error: syntax error, unexpected T_LNUMBER in C:\Program Files\thepath\movie_details.php on line 107

and this is the code (a little b4 and after the error location) with lines 107 highlighted :


$movie_health = calculate_differences($movie_takings, $movie_cost);
$page_start =
"<html>
<head>
<title>Details and Reviews for: $movie_name</title>
</head>
<body>
";

$movie_details =
"<table width="70%" border="0" cellspacing="2" cellpadding="2" align="center">
<tr>
<th colspan="6"><u><h2>$movie_name: Details</h2></u></th>
</tr>
$movie_table_headings
<tr>
<td width="33%" align="center">$movie_name</td>
<td align="center">$movie_year</td>
<td align="center">$director</td>
<td align="center">$leadactor</td>
<td align="center">$movie_running_time</td>
<td align="center">$movie_health</td>
</tr>
</table>
<br>
<br>
";


Now, i have rechecked for typo errors 3 times - could it have to do with using " instead of HEREDOC? The tutorail scripts have been woking fine till this. Thanks for any help, it is greatly appreciated.

Posted: Sun Jun 04, 2006 5:16 pm
by jayshields
If you want to use double quotes inside of a double quoted section of text you must escape the nested double quotes with a preceeding backslash. A better way to group text with nested double quotes is to enclose it in single quotes, in this case, you will not have to escape the double quotes, although you will not be able to handle variables properly.

For example:

Code: Select all

echo "he said "yo" and then left";
This is what you have done, the section of text is ended before 'yo', giving an error.

Code: Select all

echo "he said \"yo\" and then left";
This is what you should do to fix the problem.

Code: Select all

echo 'he said "yo" and then left';
This is what you could do in a similar situation to avoid escaping characters.

Sadly, that's not the end of it.

Code: Select all

$name = 'dave';
echo 'John looked at $name and smiled.'; //Will print exactly what's in the quotes.
echo "John looked at $name and smiled."; //Will print John looked at dave and smiled.
echo 'John looked at ' . $name . ' and smiled.'; //Is how to work-around it.