WhiteRau Posted May 30, 2011 Share Posted May 30, 2011 tore the forum apart looking for a solution to this one. for some reason, this is generating an 'unexpected $end' error. this is ALL the code. in entirety. i was just messing around getting familiar with filesystem stuff. no short tags. no curly brackets. so... <?php $fileHandler = fopen("someFile.txt", 'w') or die ("could not create file"); $text = <<<_END drop some text and some stuff plus this stuff some other stuff _END; fwrite($fileHandler, $text) or die ("could not write to file"); fclose($fileHandler); echo "File 'someFile.txt' written successfully"; ?> thanks people. this has be stumped. not like its complicated, eh? and as you can see, screws up the colour coding as well... but WHY!?!? WR! Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 30, 2011 Share Posted May 30, 2011 The $end error occurs anytime php reaches the end of your file while it is still expecting some php syntax. This can happen for any un-closed php syntax, including a string that is not closed. In your case, it is because the heredoc string is not closed because the ending heredoc tag must start in column 1. Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1222264 Share on other sites More sharing options...
WhiteRau Posted May 30, 2011 Author Share Posted May 30, 2011 ...column 1? how would i rewrite it? WR! Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1222269 Share on other sites More sharing options...
salathe Posted May 30, 2011 Share Posted May 30, 2011 There should be no space before _END; Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1222278 Share on other sites More sharing options...
WhiteRau Posted May 30, 2011 Author Share Posted May 30, 2011 ...mmmmm nope. that's not it. i put the _END right after 'some other stuff': <?php $fileHandler = fopen("someFile.txt", 'w') or die ("could not create file"); $text = <<<_END Line1 Line2 Line3 some other stuff_END; fwrite($fileHandler, $text) or die ("could not write to file"); fclose($fileHandler); echo "File 'someFile.txt' written successfully"; ?> and i still get the error. any other ideas? WR! Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1222282 Share on other sites More sharing options...
imperium2335 Posted May 30, 2011 Share Posted May 30, 2011 What is <<<_END? Shouldn't it just be: <?php $fileHandler = fopen("someFile.txt", 'w') or die ("could not create file"); $text ="Line1\nLine2\nLine3\nsome other stuff" ; fwrite($fileHandler, $text) or die ("could not write to file"); fclose($fileHandler); echo "File 'someFile.txt' written successfully"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1222283 Share on other sites More sharing options...
WhiteRau Posted May 30, 2011 Author Share Posted May 30, 2011 yup. that's it. just figured that out too. i was using a code snippet from O'Reilly's site. so figure. the guys that write the damn books can't even get their own code right... sheesh. WR! Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1222291 Share on other sites More sharing options...
PFMaBiSmAd Posted May 30, 2011 Share Posted May 30, 2011 It's likely that the O'Reilly code is correct, but as stated in this thread, the ending heredoc tag in the code you posted does not start in column 1. Ref: http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1222311 Share on other sites More sharing options...
salathe Posted May 30, 2011 Share Posted May 30, 2011 ...mmmmm nope. that's not it. i put the _END right after 'some other stuff': Wow. $text = <<<_END drop some text and some stuff plus this stuff some other stuff _END; Should have been: $text = <<<_END drop some text and some stuff plus this stuff some other stuff _END; Note the lack of a tab character (or any other space) before _END; This is all very clearly outlined in the PHP manual, see PFMaBiSmAd's latest post. Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1222315 Share on other sites More sharing options...
WhiteRau Posted May 31, 2011 Author Share Posted May 31, 2011 yeah. i see what you're saying. i just copy/pasted. should've looked @ the white space. thank you so much for clarifying. both methods work, i like the original a bit better. man, i love this site. WR! Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1222722 Share on other sites More sharing options...
WhiteRau Posted May 31, 2011 Author Share Posted May 31, 2011 okay... i lied. this DOES NOT work: <?php $fileHandler = fopen("someFile.txt", 'w') or die ("could not create file"); $text = <<<END what the heck? this should work! _END; fwrite($fileHandler, $text) or die ("could not write to file"); fclose($fileHandler); echo "File 'someFile.txt' written successfully"; ?> and it should... what's wrong? WR! Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1223000 Share on other sites More sharing options...
salathe Posted May 31, 2011 Share Posted May 31, 2011 The start tag of the heredoc block is END but the end tag is _END. They need to match. Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1223001 Share on other sites More sharing options...
WhiteRau Posted May 31, 2011 Author Share Posted May 31, 2011 i want to club myself like a baby seal... sorry. that was a stupid mistake. issue closed... for good this time... WR! Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1223014 Share on other sites More sharing options...
matthew9090 Posted May 31, 2011 Share Posted May 31, 2011 does the text need to be in quotes <?php $fileHandler = fopen("someFile.txt", 'w') or die ("could not create file"); $text = "<<<END what the heck? this should work! _END"; fwrite($fileHandler, $text) or die ("could not write to file"); fclose($fileHandler); echo "File 'someFile.txt' written successfully"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1223015 Share on other sites More sharing options...
PFMaBiSmAd Posted May 31, 2011 Share Posted May 31, 2011 It's not text. It's heredoc string syntax. A link to the documentation was posted in reply #7 in this thread. Quote Link to comment https://forums.phpfreaks.com/topic/237854-weird-end-error/#findComment-1223019 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.