Page 1 of 1

strange unexpected $end

Posted: Tue Nov 27, 2007 10:30 pm
by s.dot
I know this usually indicates a missing curly brace. But I've went through and matched mine up (of course there's room for human error there).

Anyways, here's this snippet of code

Code: Select all

private function _getPHPCode()
{
	if ($this->_PHPOnly)
	{
		//multiple php blocks, or single php block with closing tag, matches <? or <?php or <% and %> or ?>
		if (preg_match_all("/<((\?|%)|(php)?).+?(\?|%)>/sm", $this->_code, $matches))
		{
			$this->_code = implode("\n", $matches[0]);
		}
	}
}
With that code not commented out, I get Parse error: syntax error, unexpected $end

So then I comment that entire function out with /* */ comment

Code: Select all

/*private function _getPHPCode()
{
	if ($this->_PHPOnly)
	{
		//multiple php blocks, or single php block with closing tag, matches <? or <?php or <% and %> or ?>
		if (preg_match_all("/<((\?|%)|(php)?).+?(\?|%)>/sm", $this->_code, $matches))
		{
			$this->_code = implode("\n", $matches[0]);
		}
	}
}*/
Now the script works, except of course, it can't use that function. Funny thing is, when I comment the whole function out with // style comments like this:

Code: Select all

//private function _getPHPCode()
//{
//	if ($this->_PHPOnly)
//	{
//		//multiple php blocks, or single php block with closing tag, matches <? or <?php or <% and %> or ?>
//		if (preg_match_all("/<((\?|%)|(php)?).+?(\?|%)>/sm", $this->_code, $matches))
//		{
//			$this->_code = implode("\n", $matches[0]);
//		}
//	}
//}
I get this error: Parse error: syntax error, unexpected ';', expecting T_FUNCTION. Weird.. I didn't know the comment style mattered.

Next, I comment out only the "meat" of the function using //:

Code: Select all

private function _getPHPCode()
{
	//if ($this->_PHPOnly)
	//{
	//	//multiple php blocks, or single php block with closing tag, matches <? or <?php or <% and %> or ?>
	//	if (preg_match_all("/<((\?|%)|(php)?).+?(\?|%)>/sm", $this->_code, $matches))
	//	{
	//		$this->_code = implode("\n", $matches[0]);
	//	}
	//}
}
Now I get Parse error: syntax error, unexpected $end

If I comment out the "meat" of the function using /* */ like this

Code: Select all

private function _getPHPCode()
{
	/*if ($this->_PHPOnly)
	{
		//multiple php blocks, or single php block with closing tag, matches <? or <?php or <% and %> or ?>
		if (preg_match_all("/<((\?|%)|(php)?).+?(\?|%)>/sm", $this->_code, $matches))
		{
			$this->_code = implode("\n", $matches[0]);
		}
	}*/
}
The code will run (but of course, can't use this function).

What the heck is going on here? I was very careful to make sure exactly what I put in this post is exactly what I'm doing.

Posted: Tue Nov 27, 2007 10:38 pm
by John Cartwright
Wow that is really weird.

When I copy this into my editor the code looks like

//multiple php blocks, or single php block with closing tag, matches <? or <?php or <% and %> or ?>

Indicating that the closing ?> php tag is terminating the comment. Removing the last ?> fixes things. Don't ask me why.. hopefully somebody can figure this one out.

Posted: Tue Nov 27, 2007 10:59 pm
by s.dot
Wow, that really is weird. I removed that from my comment and it fixed the script.

Maybe it's a PHP bug that // style comments can't end with ?> :?:

Posted: Tue Nov 27, 2007 11:02 pm
by s.dot
Making a test file to reproduce:

Code: Select all

<?php

//hi, i'm a slash style comment ending with ?>
echo 'hi!';
The output is "echo 'hi';" instead of "hi"

Second test file:

Code: Select all

<?php

class test
{
	//this comment ends with ?>
	public function hello()
	{
		return print 'hell';
	}
}

$test = new test();
$test->hello();
Results in Parse error: syntax error, unexpected ';', expecting T_FUNCTION in C:\Apache2\Apache2\htdocs\crap\bug.php on line 5

Posted: Tue Nov 27, 2007 11:47 pm
by s.dot
I would file a bug report but: http://bugs.php.net/bug.php?id=10311

I guess this is expected behavior.

Posted: Tue Nov 27, 2007 11:54 pm
by John Cartwright
Good find. Interesting to see how I've never seen such a thing occur before.

Posted: Wed Nov 28, 2007 12:18 am
by s.dot
I filed a bug report anyways =/ (probably annoys them)
But I don't see how a sequence of characters in a comment can denote script termination. And if so, why is it only // style comments? I think a comment should be a comment, nothing more (because that caused me an hours worth of headaches).

I'm probably about to get owned by the people at php.net :P