Forum Moderators: coopster

Message Too Old, No Replies

I'm getting a Constant Encapsed String error because of single quotes

         

robd1302

9:26 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



This is the strangest thing I've ever seen. I enclose all of my strings in double-quotes, and I've never had trouble including a single quote within the string without escaping it.

Today, when running a new script, I kept getting an error like this:

unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')'

After some testing, I realized that escaping all the single quotes in the line that produced the error would fix the problem, and the error would show up on the next line that had a single quote within the string.

That had never happened before, but I sucked it up and escaped all of the single quotes in my string assignments.

Here's the strangest part though. Once I had escaped all of the single quotes within my strings, it started coming up with an error at a place I had commented out, and with syntax that I know to be correct.

Here is a snippet of the code:

/**
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $totalfile);
fclose($fh);
**/

Not only is the area commented out, but the error only goes away when I escape all three single quotes on the second line, like:

$fh = fopen($myFile, \'w\') or die("can\'t open file");

Is that normal? I feel like there must be something else at play here, like an open multi-line string or something, but I've checked through and can't find anything like that in my code.

Has anyone ever experienced something like this?

Thanks so much for your help!

Robert

robd1302

9:53 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



I checked my code about thousand times before I posted here, and of course, 10 minutes after posting, I solved the problem.

It was a multi-line string issue. The line of code that caused the problem was similar to this:

array("don't do drugs", "you can't do drugs", "you shouldn't do drugs", please don't do drugs");

The last string in the array didn't have an open double-quote, so I guess the single quote in the word "don't" started a never-ending multi-line string, which is why it wanted me to escape all single quotes, even in commented out parts of the code.

g1smd

10:17 pm on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah, sometimes the problem is a long way from the symptoms and the error message.

Very frustrating.

I usually close the editor and do something else, and when I come back later in the day the error is obvious.