Why doesn't my script work?

A

Anonymous

Guest
Code:
else
{
print "<td><a href="check.php?month="."$month"."&year="."$year"."&date="."$dayarray[mday]">$dayarray[mday]</a></td>\n";
$start += ADAY;
}
This is about line 51, but I get the message
Parse error: parse error in /home/neo/public_html/studio/new/cal.php on line 51

HELP!!


Code:
else
{
$link = "check.php?month="."$month"."&year="."$year"."&date="."$dayarray[mday]";
print "<td><a href="$link">$dayarray[mday]</a></td>\n";
$start += ADAY;
}

I ve also tried this now, but I get the same error message..
TIA
 
frogrocker said:
Code:
else
{
print "<td><a href="check.php?month="."$month"."&year="."$year"."&date="."$dayarray[mday]">$dayarray[mday]</a></td>\n";
$start += ADAY;
}

I can think of several reasons, frogrocker. First, when assigning a string with quotation marks, you have to use a backslash (\) to escape any additional quotation marks within the string, or PHP will interpret them as the end of the string:

Code:
<?
   echo "You have to escape "quotation marks".";
   /* this will output <You have to escape "quotation marks.">
      (without the angle brackets, of course)
   */
?>

Secondly, when specifying variables in a double-quoted string, you can either just put them inside the string like this:

Code:
<?
   $a = 'pie';
   echo "I like $a, but my best friend doesn't.";
   // this will output <I like pie, but my best friend doesn't.>
?>

(There's some caveats to this, so make sure to check out the string parsing section in the documentation.)
... or you can use .-concatenation, like so:

Code:
<?
   $a = 'pie';
   echo "I don't like " . $a . " but my best friend does.";
   // this will output <I don't like pie but my best friend does."
?>

The way you're doing it will actually work, but it's overly complicated and makes PHP do unnecessary parsing. So, to put it all together, this is a version of your code that will work:

Code:
<?
else { 
   print "<td><a href="check.php?month=" . $month . "&year=" . $year . "&date=" . $dayarray['mday'] . "">" . $dayarray['mday'] . "</a></td>\n"; 
   $start += ADAY; 
}
?>

I'm assuming here that ADAY is a constant as created using constant(). I wasn't sure about mday, however, but if it's not a constant, you need to have the quotes around it as above.

Finally, I'd like to recommend that you download a text editor that will automatically highlight PHP code. This makes programming a lot less painful and helps you catch things like unescaped quotation marks while you're writing the code. I recommend TextPad, HTML-Kit, or Vim.
 
Back
Top