strange unexpected $end

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

strange unexpected $end

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 ?> :?:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I would file a bug report but: http://bugs.php.net/bug.php?id=10311

I guess this is expected behavior.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Good find. Interesting to see how I've never seen such a thing occur before.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply